Coverage Report

Created: 2025-11-11 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unit/src/nxt_external.c
Line
Count
Source
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
0
{
33
0
    int  res, flags;
34
35
0
    if (fd == -1) {
36
0
        return NXT_OK;
37
0
    }
38
39
0
    flags = fcntl(fd, F_GETFD);
40
41
0
    if (nxt_slow_path(flags == -1)) {
42
0
        nxt_alert(task, "fcntl(%d, F_GETFD) failed %E", fd, nxt_errno);
43
0
        return NXT_ERROR;
44
0
    }
45
46
0
    flags &= ~FD_CLOEXEC;
47
48
0
    res = fcntl(fd, F_SETFD, flags);
49
50
0
    if (nxt_slow_path(res == -1)) {
51
0
        nxt_alert(task, "fcntl(%d, F_SETFD) failed %E", fd, nxt_errno);
52
0
        return NXT_ERROR;
53
0
    }
54
55
0
    return NXT_OK;
56
0
}
57
58
59
static nxt_int_t
60
nxt_external_start(nxt_task_t *task, nxt_process_data_t *data)
61
0
{
62
0
    char                     **argv;
63
0
    u_char                   buf[256];
64
0
    u_char                   *p, *end;
65
0
    uint32_t                 index;
66
0
    size_t                   size;
67
0
    nxt_str_t                str;
68
0
    nxt_int_t                rc;
69
0
    nxt_uint_t               i, argc;
70
0
    nxt_port_t               *my_port, *proto_port, *router_port;
71
0
    nxt_runtime_t            *rt;
72
0
    nxt_conf_value_t         *value;
73
0
    nxt_common_app_conf_t    *conf;
74
0
    nxt_external_app_conf_t  *c;
75
76
0
    rt = task->thread->runtime;
77
0
    conf = data->app;
78
79
0
    proto_port = rt->port_by_type[NXT_PROCESS_PROTOTYPE];
80
0
    router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
81
0
    my_port = nxt_runtime_port_find(rt, nxt_pid, 0);
82
83
0
    if (nxt_slow_path(proto_port == NULL || my_port == NULL
84
0
                      || router_port == NULL))
85
0
    {
86
0
        return NXT_ERROR;
87
0
    }
88
89
0
    rc = nxt_external_fd_no_cloexec(task, proto_port->pair[1]);
90
0
    if (nxt_slow_path(rc != NXT_OK)) {
91
0
        return NXT_ERROR;
92
0
    }
93
94
0
    rc = nxt_external_fd_no_cloexec(task, router_port->pair[1]);
95
0
    if (nxt_slow_path(rc != NXT_OK)) {
96
0
        return NXT_ERROR;
97
0
    }
98
99
0
    rc = nxt_external_fd_no_cloexec(task, my_port->pair[0]);
100
0
    if (nxt_slow_path(rc != NXT_OK)) {
101
0
        return NXT_ERROR;
102
0
    }
103
104
0
    rc = nxt_external_fd_no_cloexec(task, my_port->pair[1]);
105
0
    if (nxt_slow_path(rc != NXT_OK)) {
106
0
        return NXT_ERROR;
107
0
    }
108
109
0
    rc = nxt_external_fd_no_cloexec(task, conf->shared_port_fd);
110
0
    if (nxt_slow_path(rc != NXT_OK)) {
111
0
        return NXT_ERROR;
112
0
    }
113
114
0
    rc = nxt_external_fd_no_cloexec(task, conf->shared_queue_fd);
115
0
    if (nxt_slow_path(rc != NXT_OK)) {
116
0
        return NXT_ERROR;
117
0
    }
118
119
0
    end = buf + sizeof(buf);
120
121
0
    p = nxt_sprintf(buf, end,
122
0
                    "%s;%uD;"
123
0
                    "%PI,%ud,%d;"
124
0
                    "%PI,%ud,%d;"
125
0
                    "%PI,%ud,%d,%d;"
126
0
                    "%d,%d;"
127
0
                    "%d,%z,%uD,%Z",
128
0
                    NXT_VERSION, my_port->process->stream,
129
0
                    proto_port->pid, proto_port->id, proto_port->pair[1],
130
0
                    router_port->pid, router_port->id, router_port->pair[1],
131
0
                    my_port->pid, my_port->id, my_port->pair[0],
132
0
                                               my_port->pair[1],
133
0
                    conf->shared_port_fd, conf->shared_queue_fd,
134
0
                    2, conf->shm_limit, conf->request_limit);
135
136
0
    if (nxt_slow_path(p == end)) {
137
0
        nxt_alert(task, "internal error: buffer too small for NXT_UNIT_INIT");
138
139
0
        return NXT_ERROR;
140
0
    }
141
142
0
    nxt_debug(task, "update "NXT_UNIT_INIT_ENV"=%s", buf);
143
144
0
    rc = setenv(NXT_UNIT_INIT_ENV, (char *) buf, 1);
145
0
    if (nxt_slow_path(rc == -1)) {
146
0
        nxt_alert(task, "setenv("NXT_UNIT_INIT_ENV", %s) failed %E", buf,
147
0
                  nxt_errno);
148
149
0
        return NXT_ERROR;
150
0
    }
151
152
0
    c = &conf->u.external;
153
154
0
    argc = 2;
155
0
    size = 0;
156
157
0
    if (c->arguments != NULL) {
158
159
0
        for (index = 0; /* void */ ; index++) {
160
0
            value = nxt_conf_get_array_element(c->arguments, index);
161
0
            if (value == NULL) {
162
0
                break;
163
0
            }
164
165
0
            nxt_conf_get_string(value, &str);
166
167
0
            size += str.length + 1;
168
0
            argc++;
169
0
        }
170
0
    }
171
172
0
    argv = nxt_malloc(argc * sizeof(argv[0]) + size);
173
0
    if (nxt_slow_path(argv == NULL)) {
174
0
        nxt_alert(task, "failed to allocate arguments");
175
0
        return NXT_ERROR;
176
0
    }
177
178
0
    argv[0] = c->executable;
179
0
    i = 1;
180
181
0
    if (c->arguments != NULL) {
182
0
        p = (u_char *) &argv[argc];
183
184
0
        for (index = 0; /* void */ ; index++) {
185
0
            value = nxt_conf_get_array_element(c->arguments, index);
186
0
            if (value == NULL) {
187
0
                break;
188
0
            }
189
190
0
            argv[i++] = (char *) p;
191
192
0
            nxt_conf_get_string(value, &str);
193
194
0
            p = nxt_cpymem(p, str.start, str.length);
195
0
            *p++ = '\0';
196
0
        }
197
0
    }
198
199
0
    argv[i] = NULL;
200
201
0
    (void) execve(c->executable, argv, environ);
202
203
0
    nxt_alert(task, "execve(%s) failed %E", c->executable, nxt_errno);
204
205
0
    nxt_free(argv);
206
207
0
    return NXT_ERROR;
208
0
}