xref: /unit/src/nxt_external.c (revision 2014:f8a0992944df)
1 
2 /*
3  * Copyright (C) Max Romanov
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 #include <nxt_router.h>
9 #include <nxt_unit.h>
10 
11 
12 static nxt_int_t nxt_external_start(nxt_task_t *task, nxt_process_data_t *data);
13 
14 
15 nxt_app_module_t  nxt_external_module = {
16     0,
17     NULL,
18     nxt_string("external"),
19     "*",
20     NULL,
21     0,
22     NULL,
23     nxt_external_start,
24 };
25 
26 
27 extern char  **environ;
28 
29 
30 nxt_inline nxt_int_t
nxt_external_fd_no_cloexec(nxt_task_t * task,nxt_socket_t fd)31 nxt_external_fd_no_cloexec(nxt_task_t *task, nxt_socket_t fd)
32 {
33     int  res, flags;
34 
35     if (fd == -1) {
36         return NXT_OK;
37     }
38 
39     flags = fcntl(fd, F_GETFD);
40 
41     if (nxt_slow_path(flags == -1)) {
42         nxt_alert(task, "fcntl(%d, F_GETFD) failed %E", fd, nxt_errno);
43         return NXT_ERROR;
44     }
45 
46     flags &= ~FD_CLOEXEC;
47 
48     res = fcntl(fd, F_SETFD, flags);
49 
50     if (nxt_slow_path(res == -1)) {
51         nxt_alert(task, "fcntl(%d, F_SETFD) failed %E", fd, nxt_errno);
52         return NXT_ERROR;
53     }
54 
55     return NXT_OK;
56 }
57 
58 
59 static nxt_int_t
nxt_external_start(nxt_task_t * task,nxt_process_data_t * data)60 nxt_external_start(nxt_task_t *task, nxt_process_data_t *data)
61 {
62     char                     **argv;
63     u_char                   buf[256];
64     u_char                   *p, *end;
65     uint32_t                 index;
66     size_t                   size;
67     nxt_str_t                str;
68     nxt_int_t                rc;
69     nxt_uint_t               i, argc;
70     nxt_port_t               *my_port, *proto_port, *router_port;
71     nxt_runtime_t            *rt;
72     nxt_conf_value_t         *value;
73     nxt_common_app_conf_t    *conf;
74     nxt_external_app_conf_t  *c;
75 
76     rt = task->thread->runtime;
77     conf = data->app;
78 
79     proto_port = rt->port_by_type[NXT_PROCESS_PROTOTYPE];
80     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
81     my_port = nxt_runtime_port_find(rt, nxt_pid, 0);
82 
83     if (nxt_slow_path(proto_port == NULL || my_port == NULL
84                       || router_port == NULL))
85     {
86         return NXT_ERROR;
87     }
88 
89     rc = nxt_external_fd_no_cloexec(task, proto_port->pair[1]);
90     if (nxt_slow_path(rc != NXT_OK)) {
91         return NXT_ERROR;
92     }
93 
94     rc = nxt_external_fd_no_cloexec(task, router_port->pair[1]);
95     if (nxt_slow_path(rc != NXT_OK)) {
96         return NXT_ERROR;
97     }
98 
99     rc = nxt_external_fd_no_cloexec(task, my_port->pair[0]);
100     if (nxt_slow_path(rc != NXT_OK)) {
101         return NXT_ERROR;
102     }
103 
104     rc = nxt_external_fd_no_cloexec(task, my_port->pair[1]);
105     if (nxt_slow_path(rc != NXT_OK)) {
106         return NXT_ERROR;
107     }
108 
109     rc = nxt_external_fd_no_cloexec(task, conf->shared_port_fd);
110     if (nxt_slow_path(rc != NXT_OK)) {
111         return NXT_ERROR;
112     }
113 
114     rc = nxt_external_fd_no_cloexec(task, conf->shared_queue_fd);
115     if (nxt_slow_path(rc != NXT_OK)) {
116         return NXT_ERROR;
117     }
118 
119     end = buf + sizeof(buf);
120 
121     p = nxt_sprintf(buf, end,
122                     "%s;%uD;"
123                     "%PI,%ud,%d;"
124                     "%PI,%ud,%d;"
125                     "%PI,%ud,%d,%d;"
126                     "%d,%d;"
127                     "%d,%z,%uD,%Z",
128                     NXT_VERSION, my_port->process->stream,
129                     proto_port->pid, proto_port->id, proto_port->pair[1],
130                     router_port->pid, router_port->id, router_port->pair[1],
131                     my_port->pid, my_port->id, my_port->pair[0],
132                                                my_port->pair[1],
133                     conf->shared_port_fd, conf->shared_queue_fd,
134                     2, conf->shm_limit, conf->request_limit);
135 
136     if (nxt_slow_path(p == end)) {
137         nxt_alert(task, "internal error: buffer too small for NXT_UNIT_INIT");
138 
139         return NXT_ERROR;
140     }
141 
142     nxt_debug(task, "update "NXT_UNIT_INIT_ENV"=%s", buf);
143 
144     rc = setenv(NXT_UNIT_INIT_ENV, (char *) buf, 1);
145     if (nxt_slow_path(rc == -1)) {
146         nxt_alert(task, "setenv("NXT_UNIT_INIT_ENV", %s) failed %E", buf,
147                   nxt_errno);
148 
149         return NXT_ERROR;
150     }
151 
152     c = &conf->u.external;
153 
154     argc = 2;
155     size = 0;
156 
157     if (c->arguments != NULL) {
158 
159         for (index = 0; /* void */ ; index++) {
160             value = nxt_conf_get_array_element(c->arguments, index);
161             if (value == NULL) {
162                 break;
163             }
164 
165             nxt_conf_get_string(value, &str);
166 
167             size += str.length + 1;
168             argc++;
169         }
170     }
171 
172     argv = nxt_malloc(argc * sizeof(argv[0]) + size);
173     if (nxt_slow_path(argv == NULL)) {
174         nxt_alert(task, "failed to allocate arguments");
175         return NXT_ERROR;
176     }
177 
178     argv[0] = c->executable;
179     i = 1;
180 
181     if (c->arguments != NULL) {
182         p = (u_char *) &argv[argc];
183 
184         for (index = 0; /* void */ ; index++) {
185             value = nxt_conf_get_array_element(c->arguments, index);
186             if (value == NULL) {
187                 break;
188             }
189 
190             argv[i++] = (char *) p;
191 
192             nxt_conf_get_string(value, &str);
193 
194             p = nxt_cpymem(p, str.start, str.length);
195             *p++ = '\0';
196         }
197     }
198 
199     argv[i] = NULL;
200 
201     (void) execve(c->executable, argv, environ);
202 
203     nxt_alert(task, "execve(%s) failed %E", c->executable, nxt_errno);
204 
205     nxt_free(argv);
206 
207     return NXT_ERROR;
208 }
209