xref: /unit/src/nxt_external.c (revision 1489:4a3ec07f4b19)
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;
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     my_port = nxt_runtime_port_find(rt, nxt_pid, 0);
83 
84     if (nxt_slow_path(main_port == NULL || my_port == NULL)) {
85         return NXT_ERROR;
86     }
87 
88     rc = nxt_external_fd_no_cloexec(task, main_port->pair[1]);
89     if (nxt_slow_path(rc != NXT_OK)) {
90         return NXT_ERROR;
91     }
92 
93     rc = nxt_external_fd_no_cloexec(task, my_port->pair[0]);
94     if (nxt_slow_path(rc != NXT_OK)) {
95         return NXT_ERROR;
96     }
97 
98     end = buf + sizeof(buf);
99 
100     p = nxt_sprintf(buf, end,
101                     "%s;%uD;"
102                     "%PI,%ud,%d;"
103                     "%PI,%ud,%d;"
104                     "%d,%z,%Z",
105                     NXT_VERSION, my_port->process->stream,
106                     main_port->pid, main_port->id, main_port->pair[1],
107                     my_port->pid, my_port->id, my_port->pair[0],
108                     2, conf->shm_limit);
109 
110     if (nxt_slow_path(p == end)) {
111         nxt_alert(task, "internal error: buffer too small for NXT_UNIT_INIT");
112 
113         return NXT_ERROR;
114     }
115 
116     nxt_debug(task, "update "NXT_UNIT_INIT_ENV"=%s", buf);
117 
118     rc = setenv(NXT_UNIT_INIT_ENV, (char *) buf, 1);
119     if (nxt_slow_path(rc == -1)) {
120         nxt_alert(task, "setenv("NXT_UNIT_INIT_ENV", %s) failed %E", buf,
121                   nxt_errno);
122 
123         return NXT_ERROR;
124     }
125 
126     c = &conf->u.external;
127 
128     argc = 2;
129     size = 0;
130 
131     if (c->arguments != NULL) {
132 
133         for (index = 0; /* void */ ; index++) {
134             value = nxt_conf_get_array_element(c->arguments, index);
135             if (value == NULL) {
136                 break;
137             }
138 
139             nxt_conf_get_string(value, &str);
140 
141             size += str.length + 1;
142             argc++;
143         }
144     }
145 
146     argv = nxt_malloc(argc * sizeof(argv[0]) + size);
147     if (nxt_slow_path(argv == NULL)) {
148         nxt_alert(task, "failed to allocate arguments");
149         return NXT_ERROR;
150     }
151 
152     argv[0] = c->executable;
153     i = 1;
154 
155     if (c->arguments != NULL) {
156         p = (u_char *) &argv[argc];
157 
158         for (index = 0; /* void */ ; index++) {
159             value = nxt_conf_get_array_element(c->arguments, index);
160             if (value == NULL) {
161                 break;
162             }
163 
164             argv[i++] = (char *) p;
165 
166             nxt_conf_get_string(value, &str);
167 
168             p = nxt_cpymem(p, str.start, str.length);
169             *p++ = '\0';
170         }
171     }
172 
173     argv[i] = NULL;
174 
175     (void) execve(c->executable, argv, environ);
176 
177     nxt_alert(task, "execve(%s) failed %E", c->executable, nxt_errno);
178 
179     nxt_free(argv);
180 
181     return NXT_ERROR;
182 }
183