xref: /unit/src/nxt_external.c (revision 804:fe8d2dea28dd)
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_init(nxt_task_t *task,
13     nxt_common_app_conf_t *conf);
14 
15 
16 nxt_app_module_t  nxt_external_module = {
17     0,
18     NULL,
19     nxt_string("external"),
20     "*",
21     nxt_external_init,
22 };
23 
24 
25 extern char  **environ;
26 
27 
28 nxt_inline nxt_int_t
29 nxt_external_fd_no_cloexec(nxt_task_t *task, nxt_socket_t fd)
30 {
31     int  res, flags;
32 
33     if (fd == -1) {
34         return NXT_OK;
35     }
36 
37     flags = fcntl(fd, F_GETFD);
38 
39     if (nxt_slow_path(flags == -1)) {
40         nxt_alert(task, "fcntl(%d, F_GETFD) failed %E", fd, nxt_errno);
41         return NXT_ERROR;
42     }
43 
44     flags &= ~FD_CLOEXEC;
45 
46     res = fcntl(fd, F_SETFD, flags);
47 
48     if (nxt_slow_path(res == -1)) {
49         nxt_alert(task, "fcntl(%d, F_SETFD) failed %E", fd, nxt_errno);
50         return NXT_ERROR;
51     }
52 
53     nxt_fd_blocking(task, fd);
54 
55     return NXT_OK;
56 }
57 
58 
59 static nxt_int_t
60 nxt_external_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
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, *main_port;
71     nxt_runtime_t            *rt;
72     nxt_conf_value_t         *value;
73     nxt_external_app_conf_t  *c;
74 
75     rt = task->thread->runtime;
76 
77     main_port = rt->port_by_type[NXT_PROCESS_MAIN];
78     my_port = nxt_runtime_port_find(rt, nxt_pid, 0);
79 
80     if (nxt_slow_path(main_port == NULL || my_port == NULL)) {
81         return NXT_ERROR;
82     }
83 
84     rc = nxt_external_fd_no_cloexec(task, main_port->pair[1]);
85     if (nxt_slow_path(rc != NXT_OK)) {
86         return NXT_ERROR;
87     }
88 
89     rc = nxt_external_fd_no_cloexec(task, my_port->pair[0]);
90     if (nxt_slow_path(rc != NXT_OK)) {
91         return NXT_ERROR;
92     }
93 
94     end = buf + sizeof(buf);
95 
96     p = nxt_sprintf(buf, end,
97                     "%s;%uD;"
98                     "%PI,%ud,%d;"
99                     "%PI,%ud,%d;"
100                     "%d,%Z",
101                     NXT_VERSION, my_port->process->init->stream,
102                     main_port->pid, main_port->id, main_port->pair[1],
103                     my_port->pid, my_port->id, my_port->pair[0],
104                     2);
105 
106     if (nxt_slow_path(p == end)) {
107         nxt_alert(task, "internal error: buffer too small for NXT_UNIT_INIT");
108 
109         return NXT_ERROR;
110     }
111 
112     nxt_debug(task, "update "NXT_UNIT_INIT_ENV"=%s", buf);
113 
114     rc = setenv(NXT_UNIT_INIT_ENV, (char *) buf, 1);
115     if (nxt_slow_path(rc == -1)) {
116         nxt_alert(task, "setenv("NXT_UNIT_INIT_ENV", %s) failed %E", buf,
117                   nxt_errno);
118 
119         return NXT_ERROR;
120     }
121 
122     c = &conf->u.external;
123 
124     argc = 2;
125     size = 0;
126 
127     if (c->arguments != NULL) {
128 
129         for (index = 0; /* void */ ; index++) {
130             value = nxt_conf_get_array_element(c->arguments, index);
131             if (value == NULL) {
132                 break;
133             }
134 
135             nxt_conf_get_string(value, &str);
136 
137             size += str.length + 1;
138             argc++;
139         }
140     }
141 
142     argv = nxt_malloc(argc * sizeof(argv[0]) + size);
143     if (nxt_slow_path(argv == NULL)) {
144         nxt_alert(task, "failed to allocate arguments");
145         return NXT_ERROR;
146     }
147 
148     argv[0] = c->executable;
149     i = 1;
150 
151     if (c->arguments != NULL) {
152         p = (u_char *) &argv[argc];
153 
154         for (index = 0; /* void */ ; index++) {
155             value = nxt_conf_get_array_element(c->arguments, index);
156             if (value == NULL) {
157                 break;
158             }
159 
160             argv[i++] = (char *) p;
161 
162             nxt_conf_get_string(value, &str);
163 
164             p = nxt_cpymem(p, str.start, str.length);
165             *p++ = '\0';
166         }
167     }
168 
169     argv[i] = NULL;
170 
171     (void) execve(c->executable, argv, environ);
172 
173     nxt_alert(task, "execve(%s) failed %E", c->executable, nxt_errno);
174 
175     nxt_free(argv);
176 
177     return NXT_ERROR;
178 }
179