xref: /unit/src/nxt_external.c (revision 1543:42f27153db91)
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
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     nxt_fd_blocking(task, fd);
56 
57     return NXT_OK;
58 }
59 
60 
61 static nxt_int_t
62 nxt_external_start(nxt_task_t *task, nxt_process_data_t *data)
63 {
64     char                     **argv;
65     u_char                   buf[256];
66     u_char                   *p, *end;
67     uint32_t                 index;
68     size_t                   size;
69     nxt_str_t                str;
70     nxt_int_t                rc;
71     nxt_uint_t               i, argc;
72     nxt_port_t               *my_port, *main_port, *router_port;
73     nxt_runtime_t            *rt;
74     nxt_conf_value_t         *value;
75     nxt_common_app_conf_t    *conf;
76     nxt_external_app_conf_t  *c;
77 
78     rt = task->thread->runtime;
79     conf = data->app;
80 
81     main_port = rt->port_by_type[NXT_PROCESS_MAIN];
82     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
83     my_port = nxt_runtime_port_find(rt, nxt_pid, 0);
84 
85     if (nxt_slow_path(main_port == NULL || my_port == NULL
86                       || router_port == NULL))
87     {
88         return NXT_ERROR;
89     }
90 
91     rc = nxt_external_fd_no_cloexec(task, main_port->pair[1]);
92     if (nxt_slow_path(rc != NXT_OK)) {
93         return NXT_ERROR;
94     }
95 
96     rc = nxt_external_fd_no_cloexec(task, router_port->pair[1]);
97     if (nxt_slow_path(rc != NXT_OK)) {
98         return NXT_ERROR;
99     }
100 
101     rc = nxt_external_fd_no_cloexec(task, my_port->pair[0]);
102     if (nxt_slow_path(rc != NXT_OK)) {
103         return NXT_ERROR;
104     }
105 
106     end = buf + sizeof(buf);
107 
108     p = nxt_sprintf(buf, end,
109                     "%s;%uD;"
110                     "%PI,%ud,%d;"
111                     "%PI,%ud,%d;"
112                     "%PI,%ud,%d;"
113                     "%d,%z,%Z",
114                     NXT_VERSION, my_port->process->stream,
115                     main_port->pid, main_port->id, main_port->pair[1],
116                     router_port->pid, router_port->id, router_port->pair[1],
117                     my_port->pid, my_port->id, my_port->pair[0],
118                     2, conf->shm_limit);
119 
120     if (nxt_slow_path(p == end)) {
121         nxt_alert(task, "internal error: buffer too small for NXT_UNIT_INIT");
122 
123         return NXT_ERROR;
124     }
125 
126     nxt_debug(task, "update "NXT_UNIT_INIT_ENV"=%s", buf);
127 
128     rc = setenv(NXT_UNIT_INIT_ENV, (char *) buf, 1);
129     if (nxt_slow_path(rc == -1)) {
130         nxt_alert(task, "setenv("NXT_UNIT_INIT_ENV", %s) failed %E", buf,
131                   nxt_errno);
132 
133         return NXT_ERROR;
134     }
135 
136     c = &conf->u.external;
137 
138     argc = 2;
139     size = 0;
140 
141     if (c->arguments != NULL) {
142 
143         for (index = 0; /* void */ ; index++) {
144             value = nxt_conf_get_array_element(c->arguments, index);
145             if (value == NULL) {
146                 break;
147             }
148 
149             nxt_conf_get_string(value, &str);
150 
151             size += str.length + 1;
152             argc++;
153         }
154     }
155 
156     argv = nxt_malloc(argc * sizeof(argv[0]) + size);
157     if (nxt_slow_path(argv == NULL)) {
158         nxt_alert(task, "failed to allocate arguments");
159         return NXT_ERROR;
160     }
161 
162     argv[0] = c->executable;
163     i = 1;
164 
165     if (c->arguments != NULL) {
166         p = (u_char *) &argv[argc];
167 
168         for (index = 0; /* void */ ; index++) {
169             value = nxt_conf_get_array_element(c->arguments, index);
170             if (value == NULL) {
171                 break;
172             }
173 
174             argv[i++] = (char *) p;
175 
176             nxt_conf_get_string(value, &str);
177 
178             p = nxt_cpymem(p, str.start, str.length);
179             *p++ = '\0';
180         }
181     }
182 
183     argv[i] = NULL;
184 
185     (void) execve(c->executable, argv, environ);
186 
187     nxt_alert(task, "execve(%s) failed %E", c->executable, nxt_errno);
188 
189     nxt_free(argv);
190 
191     return NXT_ERROR;
192 }
193