xref: /unit/src/nxt_external.c (revision 1998:c8790d2a89bb)
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     return NXT_OK;
56 }
57 
58 
59 static nxt_int_t
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     end = buf + sizeof(buf);
110 
111     p = nxt_sprintf(buf, end,
112                     "%s;%uD;"
113                     "%PI,%ud,%d;"
114                     "%PI,%ud,%d;"
115                     "%PI,%ud,%d,%d;"
116                     "%d,%z,%uD,%Z",
117                     NXT_VERSION, my_port->process->stream,
118                     proto_port->pid, proto_port->id, proto_port->pair[1],
119                     router_port->pid, router_port->id, router_port->pair[1],
120                     my_port->pid, my_port->id, my_port->pair[0],
121                                                my_port->pair[1],
122                     2, conf->shm_limit, conf->request_limit);
123 
124     if (nxt_slow_path(p == end)) {
125         nxt_alert(task, "internal error: buffer too small for NXT_UNIT_INIT");
126 
127         return NXT_ERROR;
128     }
129 
130     nxt_debug(task, "update "NXT_UNIT_INIT_ENV"=%s", buf);
131 
132     rc = setenv(NXT_UNIT_INIT_ENV, (char *) buf, 1);
133     if (nxt_slow_path(rc == -1)) {
134         nxt_alert(task, "setenv("NXT_UNIT_INIT_ENV", %s) failed %E", buf,
135                   nxt_errno);
136 
137         return NXT_ERROR;
138     }
139 
140     c = &conf->u.external;
141 
142     argc = 2;
143     size = 0;
144 
145     if (c->arguments != NULL) {
146 
147         for (index = 0; /* void */ ; index++) {
148             value = nxt_conf_get_array_element(c->arguments, index);
149             if (value == NULL) {
150                 break;
151             }
152 
153             nxt_conf_get_string(value, &str);
154 
155             size += str.length + 1;
156             argc++;
157         }
158     }
159 
160     argv = nxt_malloc(argc * sizeof(argv[0]) + size);
161     if (nxt_slow_path(argv == NULL)) {
162         nxt_alert(task, "failed to allocate arguments");
163         return NXT_ERROR;
164     }
165 
166     argv[0] = c->executable;
167     i = 1;
168 
169     if (c->arguments != NULL) {
170         p = (u_char *) &argv[argc];
171 
172         for (index = 0; /* void */ ; index++) {
173             value = nxt_conf_get_array_element(c->arguments, index);
174             if (value == NULL) {
175                 break;
176             }
177 
178             argv[i++] = (char *) p;
179 
180             nxt_conf_get_string(value, &str);
181 
182             p = nxt_cpymem(p, str.start, str.length);
183             *p++ = '\0';
184         }
185     }
186 
187     argv[i] = NULL;
188 
189     (void) execve(c->executable, argv, environ);
190 
191     nxt_alert(task, "execve(%s) failed %E", c->executable, nxt_errno);
192 
193     nxt_free(argv);
194 
195     return NXT_ERROR;
196 }
197