summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_go.c
blob: 329a3cd31640fec3609c439728d9463791aff6f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

/*
 * Copyright (C) Max Romanov
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>
#include <nxt_application.h>


static nxt_int_t nxt_go_init(nxt_task_t *task, nxt_common_app_conf_t *conf);

static nxt_int_t nxt_go_run(nxt_task_t *task,
                      nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *msg);

nxt_application_module_t  nxt_go_module = {
    0,
    NULL,
    nxt_string("go"),
    nxt_string("go"),
    nxt_go_init,
    nxt_go_run,
    NULL,
};


extern char  **environ;

nxt_inline nxt_int_t
nxt_go_fd_no_cloexec(nxt_task_t *task, nxt_socket_t fd)
{
    int  res, flags;

    if (fd == -1) {
        return NXT_OK;
    }

    flags = fcntl(fd, F_GETFD);

    if (nxt_slow_path(flags == -1)) {
        nxt_log(task, NXT_LOG_CRIT, "fcntl(%d, F_GETFD) failed %E",
                fd, nxt_errno);
        return NXT_ERROR;
    }

    flags &= ~FD_CLOEXEC;

    res = fcntl(fd, F_SETFD, flags);

    if (nxt_slow_path(res == -1)) {
        nxt_log(task, NXT_LOG_CRIT, "fcntl(%d, F_SETFD) failed %E",
                fd, nxt_errno);
        return NXT_ERROR;
    }

    return NXT_OK;
}


static nxt_int_t
nxt_go_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
{
    char               *argv[2];
    u_char             buf[256];
    u_char             *p, *end;
    nxt_int_t          rc;
    nxt_port_t         *my_port, *main_port;
    nxt_runtime_t      *rt;
    nxt_go_app_conf_t  *c;

    rt = task->thread->runtime;

    main_port = rt->port_by_type[NXT_PROCESS_MAIN];
    my_port = nxt_runtime_port_find(rt, nxt_pid, 0);

    if (nxt_slow_path(main_port == NULL || my_port == NULL)) {
        return NXT_ERROR;
    }

    rc = nxt_go_fd_no_cloexec(task, main_port->pair[1]);
    if (nxt_slow_path(rc != NXT_OK)) {
        return NXT_ERROR;
    }

    rc = nxt_go_fd_no_cloexec(task, my_port->pair[0]);
    if (nxt_slow_path(rc != NXT_OK)) {
        return NXT_ERROR;
    }

    end = buf + sizeof(buf);

    p = nxt_sprintf(buf, end,
                    "%s;%uD;"
                    "%PI,%ud,%d,%d,%d;"
                    "%PI,%ud,%d,%d,%d%Z",
                    NXT_VERSION, my_port->process->init->stream,
                    main_port->pid, main_port->id, (int) main_port->type,
                    -1, main_port->pair[1],
                    my_port->pid, my_port->id, (int) my_port->type,
                    my_port->pair[0], -1);

    if (nxt_slow_path(p == end)) {
        nxt_log(task, NXT_LOG_ALERT,
                "internal error: buffer too small for NXT_GO_PORTS");

        return NXT_ERROR;
    }

    nxt_debug(task, "update NXT_GO_PORTS=%s", buf);

    rc = setenv("NXT_GO_PORTS", (char *) buf, 1);
    if (nxt_slow_path(rc == -1)) {
        nxt_log(task, NXT_LOG_CRIT, "setenv(NXT_GO_PORTS, %s) failed %E",
                buf, nxt_errno);

        return NXT_ERROR;
    }

    c = &conf->u.go;

    argv[0] = c->executable;
    argv[1] = NULL;

    (void) execve(c->executable, argv, environ);

    nxt_log(task, NXT_LOG_CRIT, "execve(%s) failed %E",
            c->executable, nxt_errno);

    return NXT_ERROR;
}


static nxt_int_t
nxt_go_run(nxt_task_t *task,
           nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *msg)
{
    return NXT_ERROR;
}