Skip to content

Commit

Permalink
Core: "-e" command line option.
Browse files Browse the repository at this point in the history
When installing or running from a non-root user it is sometimes required to
override default, compiled in error log path. There was no way to do this
without rebuilding the binary (ticket #147).

This patch introduced "-e" command line option which allows one to override
compiled in error log path.
  • Loading branch information
Igor Ippolitov committed Nov 19, 2020
1 parent 23597e9 commit 7e1637a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 17 deletions.
4 changes: 4 additions & 0 deletions auto/configure
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ have=NGX_PID_PATH value="\"$NGX_PID_PATH\"" . auto/define
have=NGX_LOCK_PATH value="\"$NGX_LOCK_PATH\"" . auto/define
have=NGX_ERROR_LOG_PATH value="\"$NGX_ERROR_LOG_PATH\"" . auto/define

if [ ".$NGX_ERROR_LOG_PATH" = "." ]; then
have=NGX_ERROR_LOG_STDERR . auto/have
fi

have=NGX_HTTP_LOG_PATH value="\"$NGX_HTTP_LOG_PATH\"" . auto/define
have=NGX_HTTP_CLIENT_TEMP_PATH value="\"$NGX_HTTP_CLIENT_TEMP_PATH\""
. auto/define
Expand Down
9 changes: 8 additions & 1 deletion docs/man/nginx.8
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
.\" SUCH DAMAGE.
.\"
.\"
.Dd December 5, 2019
.Dd November 5, 2020
.Dt NGINX 8
.Os
.Sh NAME
Expand All @@ -35,6 +35,7 @@
.Nm
.Op Fl ?hqTtVv
.Op Fl c Ar file
.Op Fl e Ar file
.Op Fl g Ar directives
.Op Fl p Ar prefix
.Op Fl s Ar signal
Expand All @@ -54,6 +55,12 @@ Print help.
.It Fl c Ar file
Use an alternative configuration
.Ar file .
.It Fl e Ar file
Use an alternative error log
.Ar file .
Special value
.Cm stderr
indicates that the standard error output should be used.
.It Fl g Ar directives
Set global configuration directives.
See
Expand Down
41 changes: 37 additions & 4 deletions src/core/nginx.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ static ngx_uint_t ngx_show_help;
static ngx_uint_t ngx_show_version;
static ngx_uint_t ngx_show_configure;
static u_char *ngx_prefix;
static u_char *ngx_error_log;
static u_char *ngx_conf_file;
static u_char *ngx_conf_params;
static char *ngx_signal;
Expand Down Expand Up @@ -230,7 +231,7 @@ main(int argc, char *const *argv)
ngx_pid = ngx_getpid();
ngx_parent = ngx_getppid();

log = ngx_log_init(ngx_prefix);
log = ngx_log_init(ngx_prefix, ngx_error_log);
if (log == NULL) {
return 1;
}
Expand Down Expand Up @@ -393,9 +394,9 @@ ngx_show_version_info(void)

if (ngx_show_help) {
ngx_write_stderr(
"Usage: nginx [-?hvVtTq] [-s signal] [-c filename] "
"[-p prefix] [-g directives]" NGX_LINEFEED
NGX_LINEFEED
"Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]" NGX_LINEFEED
" [-e filename] [-c filename] [-g directives]"
NGX_LINEFEED NGX_LINEFEED
"Options:" NGX_LINEFEED
" -?,-h : this help" NGX_LINEFEED
" -v : show version and exit" NGX_LINEFEED
Expand All @@ -413,6 +414,12 @@ ngx_show_version_info(void)
NGX_LINEFEED
#else
" -p prefix : set prefix path (default: NONE)" NGX_LINEFEED
#endif
" -e filename : set error log file (default: "
#ifdef NGX_ERROR_LOG_STDERR
"stderr)" NGX_LINEFEED
#else
NGX_ERROR_LOG_PATH ")" NGX_LINEFEED
#endif
" -c filename : set configuration file (default: " NGX_CONF_PATH
")" NGX_LINEFEED
Expand Down Expand Up @@ -800,6 +807,24 @@ ngx_get_options(int argc, char *const *argv)
ngx_log_stderr(0, "option \"-p\" requires directory name");
return NGX_ERROR;

case 'e':
if (*p) {
ngx_error_log = p;

} else if (argv[++i]) {
ngx_error_log = (u_char *) argv[i];

} else {
ngx_log_stderr(0, "option \"-e\" requires file name");
return NGX_ERROR;
}

if (ngx_strcmp(ngx_error_log, "stderr") == 0) {
ngx_error_log = (u_char *) "";
}

goto next;

case 'c':
if (*p) {
ngx_conf_file = p;
Expand Down Expand Up @@ -992,6 +1017,14 @@ ngx_process_options(ngx_cycle_t *cycle)
}
}

if (ngx_error_log) {
cycle->error_log.len = ngx_strlen(ngx_error_log);
cycle->error_log.data = ngx_error_log;

} else {
ngx_str_set(&cycle->error_log, NGX_ERROR_LOG_PATH);
}

if (ngx_conf_params) {
cycle->conf_param.len = ngx_strlen(ngx_conf_params);
cycle->conf_param.data = ngx_conf_params;
Expand Down
9 changes: 9 additions & 0 deletions src/core/ngx_cycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
return NULL;
}

cycle->error_log.len = old_cycle->error_log.len;
cycle->error_log.data = ngx_pnalloc(pool, old_cycle->error_log.len + 1);
if (cycle->error_log.data == NULL) {
ngx_destroy_pool(pool);
return NULL;
}
ngx_cpystrn(cycle->error_log.data, old_cycle->error_log.data,
old_cycle->error_log.len + 1);

cycle->conf_file.len = old_cycle->conf_file.len;
cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
if (cycle->conf_file.data == NULL) {
Expand Down
1 change: 1 addition & 0 deletions src/core/ngx_cycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct ngx_cycle_s {
ngx_str_t conf_param;
ngx_str_t conf_prefix;
ngx_str_t prefix;
ngx_str_t error_log;
ngx_str_t lock_file;
ngx_str_t hostname;
};
Expand Down
19 changes: 8 additions & 11 deletions src/core/ngx_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,19 @@ ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err)


ngx_log_t *
ngx_log_init(u_char *prefix)
ngx_log_init(u_char *prefix, u_char *error_log)
{
u_char *p, *name;
size_t nlen, plen;

ngx_log.file = &ngx_log_file;
ngx_log.log_level = NGX_LOG_NOTICE;

name = (u_char *) NGX_ERROR_LOG_PATH;

/*
* we use ngx_strlen() here since BCC warns about
* condition is always false and unreachable code
*/
if (error_log == NULL) {
error_log = (u_char *) NGX_ERROR_LOG_PATH;
}

name = error_log;
nlen = ngx_strlen(name);

if (nlen == 0) {
Expand Down Expand Up @@ -369,7 +367,7 @@ ngx_log_init(u_char *prefix)
*p++ = '/';
}

ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
ngx_cpystrn(p, error_log, nlen + 1);

p = name;
}
Expand Down Expand Up @@ -403,8 +401,7 @@ ngx_log_init(u_char *prefix)
ngx_int_t
ngx_log_open_default(ngx_cycle_t *cycle)
{
ngx_log_t *log;
static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH);
ngx_log_t *log;

if (ngx_log_get_file_log(&cycle->new_log) != NULL) {
return NGX_OK;
Expand All @@ -425,7 +422,7 @@ ngx_log_open_default(ngx_cycle_t *cycle)

log->log_level = NGX_LOG_ERR;

log->file = ngx_conf_open_file(cycle, &error_log);
log->file = ngx_conf_open_file(cycle, &cycle->error_log);
if (log->file == NULL) {
return NGX_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/ngx_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void ngx_cdecl ngx_log_debug_core(ngx_log_t *log, ngx_err_t err,

/*********************************/

ngx_log_t *ngx_log_init(u_char *prefix);
ngx_log_t *ngx_log_init(u_char *prefix, u_char *error_log);
void ngx_cdecl ngx_log_abort(ngx_err_t err, const char *fmt, ...);
void ngx_cdecl ngx_log_stderr(ngx_err_t err, const char *fmt, ...);
u_char *ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err);
Expand Down

0 comments on commit 7e1637a

Please sign in to comment.