Esempio n. 1
0
void
ngx_pipe_broken_action(ngx_log_t *log, ngx_pid_t pid, ngx_int_t master)
{
    ngx_uint_t i;
#ifdef T_PIPES_OUTPUT_ON_BROKEN
    ngx_uint_t is_stderr = 0;
#endif

    for (i = 0; i < ngx_last_pipe; i++) {

        if (!ngx_pipes[i].configured) {
            continue;
        }

        if (ngx_pipes[i].generation != ngx_pipe_generation) {
            continue;
        }

        if (ngx_pipes[i].pid == pid) {

            if (close(ngx_pipes[i].open_fd->fd) == NGX_FILE_ERROR) {
                ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                              "close \"%s\" failed",
                              ngx_pipes[i].cmd);
            }

#ifdef T_PIPES_OUTPUT_ON_BROKEN
            if (ngx_pipes[i].open_fd == ngx_cycle->log->file) {
                is_stderr = 1;
            }
#endif

            ngx_pipes[i].open_fd->fd = NGX_INVALID_FILE;

#ifdef T_PIPES_OUTPUT_ON_BROKEN
            if (ngx_pipes[i].backup.len > 0 && ngx_pipes[i].backup.data != NULL) {
#else
                //just write to /dev/null
                ngx_pipes[i].backup.data = ngx_pipe_dev_null_file.data;
                ngx_pipes[i].backup.len = ngx_pipe_dev_null_file.len;
#endif
                ngx_pipes[i].open_fd->name.len = ngx_pipes[i].backup.len;
                ngx_pipes[i].open_fd->name.data = ngx_pipes[i].backup.data;

                ngx_pipes[i].open_fd->fd = ngx_open_file(ngx_pipes[i].backup.data,
                                                         NGX_FILE_APPEND,
                                                         NGX_FILE_CREATE_OR_OPEN,
                                                         NGX_FILE_DEFAULT_ACCESS);

                if (ngx_pipes[i].open_fd->fd == NGX_INVALID_FILE) {
                    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                                  ngx_open_file_n " \"%s\" failed",
                                  ngx_pipes[i].backup.data);
                }

                if (fcntl(ngx_pipes[i].open_fd->fd, F_SETFD, FD_CLOEXEC) == -1) {
                    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                                  "fcntl(FD_CLOEXEC) \"%s\" failed",
                                  ngx_pipes[i].backup.data);
                }
#ifdef T_PIPES_OUTPUT_ON_BROKEN
            }

            if (is_stderr) {
                ngx_set_stderr(ngx_cycle->log->file->fd);
            }
#endif

            if (master) {
#ifdef T_PIPES_OUTPUT_ON_BROKEN
                if (ngx_pipes[i].backup.len > 0 && ngx_pipes[i].backup.data != NULL) {
                    if (chown((const char *) ngx_pipes[i].backup.data,
                              ngx_pipes[i].user, -1)
                        == -1)
                    {
                        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                                      "chown() \"%s\" failed",
                                      ngx_pipes[i].backup.data);
                    }
                }
#endif

                ngx_signal_pipe_broken(log, pid);
            }
        }
    }
}
Esempio n. 2
0
void
ngx_pipe_broken_action(ngx_log_t *log, ngx_pid_t pid, ngx_int_t master)
{
    ngx_uint_t i, is_stderr;

    for (i = 0, is_stderr = 0; i < ngx_last_pipe; i++) {

        if (!ngx_pipes[i].configured) {
            continue;
        }

        if (ngx_pipes[i].generation != ngx_pipe_generation) {
            continue;
        }

        if (ngx_pipes[i].pid == pid) {

            if (close(ngx_pipes[i].open_fd->fd) == NGX_FILE_ERROR) {
                ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                              "close \"%s\" failed",
                              ngx_pipes[i].cmd);
            }

            if (ngx_pipes[i].open_fd == ngx_cycle->log->file) {
                is_stderr = 1;
            }

            ngx_pipes[i].open_fd->name.len = ngx_pipes[i].backup.len;
            ngx_pipes[i].open_fd->name.data = ngx_pipes[i].backup.data;

            ngx_pipes[i].open_fd->fd = ngx_open_file(ngx_pipes[i].backup.data,
                                                     NGX_FILE_APPEND,
                                                     NGX_FILE_CREATE_OR_OPEN,
                                                     NGX_FILE_DEFAULT_ACCESS);

            if (ngx_pipes[i].open_fd->fd == NGX_INVALID_FILE) {
                ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                              ngx_open_file_n " \"%s\" failed",
                              ngx_pipes[i].backup.data);
            }

            if (fcntl(ngx_pipes[i].open_fd->fd, F_SETFD, FD_CLOEXEC) == -1) {
                ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                              "fcntl(FD_CLOEXEC) \"%s\" failed",
                              ngx_pipes[i].backup.data);
            }

            if (is_stderr) {
                ngx_set_stderr(ngx_cycle->log->file->fd);
            }

            if (master) {
                if (chown((const char *) ngx_pipes[i].backup.data,
                          ngx_pipes[i].user, -1)
                    == -1)
                {
                    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                                  "chown() \"%s\" failed",
                                  ngx_pipes[i].backup.data);
                }

                ngx_signal_pipe_broken(log, pid);
            }
        }
    }
}