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