~legolas/ubuntu/natty/php5/5.3.5

« back to all changes in this revision

Viewing changes to sapi/fpm/fpm/fpm_stdio.c

  • Committer: Stas Verberkt
  • Date: 2011-02-01 09:27:15 UTC
  • Revision ID: legolas@legolasweb.nl-20110201092715-yq052iu2yl4i2eyg
Inserted PHP 5.3.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
        int fd = open("/dev/null", O_RDWR);
27
27
 
28
28
        if (0 > fd) {
29
 
                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"/dev/null\") failed");
 
29
                zlog(ZLOG_SYSERROR, "open(\"/dev/null\") failed");
30
30
                return -1;
31
31
        }
32
32
 
33
33
        if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
34
 
                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
 
34
                zlog(ZLOG_SYSERROR, "dup2() failed");
35
35
                return -1;
36
36
        }
37
37
        close(fd);
41
41
 
42
42
int fpm_stdio_init_final() /* {{{ */
43
43
{
44
 
        zlog_set_level(fpm_globals.log_level);
45
44
        if (fpm_global_config.daemonize) {
46
45
                if (fpm_globals.error_log_fd != STDERR_FILENO) {
47
46
                        /* there might be messages to stderr from libevent, we need to log them all */
48
47
                        if (0 > dup2(fpm_globals.error_log_fd, STDERR_FILENO)) {
49
 
                                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
 
48
                                zlog(ZLOG_SYSERROR, "dup2() failed");
50
49
                                return -1;
51
50
                        }
52
51
                }
53
 
                zlog_set_fd(fpm_globals.error_log_fd);
54
52
        }
 
53
        zlog_set_launched();
55
54
        return 0;
56
55
}
57
56
/* }}} */
64
63
 
65
64
        if (wp->listening_socket != STDIN_FILENO) {
66
65
                if (0 > dup2(wp->listening_socket, STDIN_FILENO)) {
67
 
                        zlog(ZLOG_STUFF, ZLOG_SYSERROR, "dup2() failed");
 
66
                        zlog(ZLOG_SYSERROR, "dup2() failed");
68
67
                        return -1;
69
68
                }
70
69
        }
72
71
}
73
72
/* }}} */
74
73
 
75
 
static void fpm_stdio_child_said(int fd, short which, void *arg) /* {{{ */
 
74
static void fpm_stdio_child_said(struct fpm_event_s *ev, short which, void *arg) /* {{{ */
76
75
{
77
76
        static const int max_buf_size = 1024;
 
77
        int fd = ev->fd;
78
78
        char buf[max_buf_size];
79
 
        struct fpm_child_s *child = arg;
80
 
        int is_stdout = fd == child->fd_stdout;
81
 
        struct event *ev = is_stdout ? &child->ev_stdout : &child->ev_stderr;
 
79
        struct fpm_child_s *child;
 
80
        int is_stdout;
 
81
        struct fpm_event_s *event;
82
82
        int fifo_in = 1, fifo_out = 1;
83
83
        int is_last_message = 0;
84
84
        int in_buf = 0;
85
85
        int res;
86
86
 
 
87
        if (!arg) {
 
88
                return;
 
89
        }
 
90
        child = (struct fpm_child_s *)arg;
 
91
        is_stdout = (fd == child->fd_stdout);
 
92
        if (is_stdout) {
 
93
                event = &child->ev_stdout;
 
94
        } else {
 
95
                event = &child->ev_stderr;
 
96
        }
 
97
 
87
98
        while (fifo_in || fifo_out) {
88
99
                if (fifo_in) {
89
100
                        res = read(fd, buf + in_buf, max_buf_size - 1 - in_buf);
94
105
                                } else { /* error or pipe is closed */
95
106
 
96
107
                                        if (res < 0) { /* error */
97
 
                                                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "read() failed");
 
108
                                                zlog(ZLOG_SYSERROR, "read() failed");
98
109
                                        }
99
110
 
100
 
                                        fpm_event_del(ev);
 
111
                                        fpm_event_del(event);
101
112
                                        is_last_message = 1;
102
113
 
103
114
                                        if (is_stdout) {
140
151
                                                *nl = '\0';
141
152
                                        }
142
153
 
143
 
                                        zlog(ZLOG_STUFF, ZLOG_WARNING, "[pool %s] child %d said into %s: \"%s\"%s", child->wp->config->name,
 
154
                                        zlog(ZLOG_WARNING, "[pool %s] child %d said into %s: \"%s\"%s", child->wp->config->name,
144
155
                                          (int) child->pid, is_stdout ? "stdout" : "stderr", buf, is_last_message ? ", pipe is closed" : "");
145
156
 
146
157
                                        if (nl) {
164
175
        }
165
176
 
166
177
        if (0 > pipe(fd_stdout)) {
167
 
                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
 
178
                zlog(ZLOG_SYSERROR, "pipe() failed");
168
179
                return -1;
169
180
        }
170
181
 
171
182
        if (0 > pipe(fd_stderr)) {
172
 
                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "pipe() failed");
 
183
                zlog(ZLOG_SYSERROR, "pipe() failed");
173
184
                close(fd_stdout[0]); close(fd_stdout[1]);
174
185
                return -1;
175
186
        }
176
187
 
177
188
        if (0 > fd_set_blocked(fd_stdout[0], 0) || 0 > fd_set_blocked(fd_stderr[0], 0)) {
178
 
                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "fd_set_blocked() failed");
 
189
                zlog(ZLOG_SYSERROR, "fd_set_blocked() failed");
179
190
                close(fd_stdout[0]); close(fd_stdout[1]);
180
191
                close(fd_stderr[0]); close(fd_stderr[1]);
181
192
                return -1;
184
195
}
185
196
/* }}} */
186
197
 
187
 
int fpm_stdio_parent_use_pipes(struct fpm_child_s *child, struct event_base *base) /* {{{ */
 
198
int fpm_stdio_parent_use_pipes(struct fpm_child_s *child) /* {{{ */
188
199
{
189
200
        if (0 == child->wp->config->catch_workers_output) { /* not required */
190
201
                return 0;
196
207
        child->fd_stdout = fd_stdout[0];
197
208
        child->fd_stderr = fd_stderr[0];
198
209
 
199
 
        fpm_event_add(child->fd_stdout, base, &child->ev_stdout, fpm_stdio_child_said, child);
200
 
        fpm_event_add(child->fd_stderr, base, &child->ev_stderr, fpm_stdio_child_said, child);
 
210
        fpm_event_set(&child->ev_stdout, child->fd_stdout, FPM_EV_READ, fpm_stdio_child_said, child);
 
211
        fpm_event_add(&child->ev_stdout, 0);
 
212
 
 
213
        fpm_event_set(&child->ev_stderr, child->fd_stderr, FPM_EV_READ, fpm_stdio_child_said, child);
 
214
        fpm_event_add(&child->ev_stderr, 0);
201
215
        return 0;
202
216
}
203
217
/* }}} */
237
251
 
238
252
        fd = open(fpm_global_config.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
239
253
        if (0 > fd) {
240
 
                zlog(ZLOG_STUFF, ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_config.error_log);
 
254
                zlog(ZLOG_SYSERROR, "open(\"%s\") failed", fpm_global_config.error_log);
241
255
                return -1;
242
256
        }
243
257
 
251
265
                fd = fpm_globals.error_log_fd; /* for FD_CLOSEXEC to work */
252
266
        } else {
253
267
                fpm_globals.error_log_fd = fd;
 
268
                if (fpm_global_config.daemonize) {
 
269
                        zlog_set_fd(fpm_globals.error_log_fd);
 
270
                }
254
271
        }
255
272
        fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
256
273
        return 0;