Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 #if (NGX_SETPROCTITLE_USES_ENV)
0013 
0014 /*
0015  * To change the process title in Linux and Solaris we have to set argv[1]
0016  * to NULL and to copy the title to the same place where the argv[0] points to.
0017  * However, argv[0] may be too small to hold a new title.  Fortunately, Linux
0018  * and Solaris store argv[] and environ[] one after another.  So we should
0019  * ensure that is the continuous memory and then we allocate the new memory
0020  * for environ[] and copy it.  After this we could use the memory starting
0021  * from argv[0] for our process title.
0022  *
0023  * The Solaris's standard /bin/ps does not show the changed process title.
0024  * You have to use "/usr/ucb/ps -w" instead.  Besides, the UCB ps does not
0025  * show a new title if its length less than the origin command line length.
0026  * To avoid it we append to a new title the origin command line in the
0027  * parenthesis.
0028  */
0029 
0030 extern char **environ;
0031 
0032 static char *ngx_os_argv_last;
0033 
0034 ngx_int_t
0035 ngx_init_setproctitle(ngx_log_t *log)
0036 {
0037     u_char      *p;
0038     size_t       size;
0039     ngx_uint_t   i;
0040 
0041     size = 0;
0042 
0043     for (i = 0; environ[i]; i++) {
0044         size += ngx_strlen(environ[i]) + 1;
0045     }
0046 
0047     p = ngx_alloc(size, log);
0048     if (p == NULL) {
0049         return NGX_ERROR;
0050     }
0051 
0052     ngx_os_argv_last = ngx_os_argv[0];
0053 
0054     for (i = 0; ngx_os_argv[i]; i++) {
0055         if (ngx_os_argv_last == ngx_os_argv[i]) {
0056             ngx_os_argv_last = ngx_os_argv[i] + ngx_strlen(ngx_os_argv[i]) + 1;
0057         }
0058     }
0059 
0060     for (i = 0; environ[i]; i++) {
0061         if (ngx_os_argv_last == environ[i]) {
0062 
0063             size = ngx_strlen(environ[i]) + 1;
0064             ngx_os_argv_last = environ[i] + size;
0065 
0066             ngx_cpystrn(p, (u_char *) environ[i], size);
0067             environ[i] = (char *) p;
0068             p += size;
0069         }
0070     }
0071 
0072     ngx_os_argv_last--;
0073 
0074     return NGX_OK;
0075 }
0076 
0077 
0078 void
0079 ngx_setproctitle(char *title)
0080 {
0081     u_char     *p;
0082 
0083 #if (NGX_SOLARIS)
0084 
0085     ngx_int_t   i;
0086     size_t      size;
0087 
0088 #endif
0089 
0090     ngx_os_argv[1] = NULL;
0091 
0092     p = ngx_cpystrn((u_char *) ngx_os_argv[0], (u_char *) "nginx: ",
0093                     ngx_os_argv_last - ngx_os_argv[0]);
0094 
0095     p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char *) p);
0096 
0097 #if (NGX_SOLARIS)
0098 
0099     size = 0;
0100 
0101     for (i = 0; i < ngx_argc; i++) {
0102         size += ngx_strlen(ngx_argv[i]) + 1;
0103     }
0104 
0105     if (size > (size_t) ((char *) p - ngx_os_argv[0])) {
0106 
0107         /*
0108          * ngx_setproctitle() is too rare operation so we use
0109          * the non-optimized copies
0110          */
0111 
0112         p = ngx_cpystrn(p, (u_char *) " (", ngx_os_argv_last - (char *) p);
0113 
0114         for (i = 0; i < ngx_argc; i++) {
0115             p = ngx_cpystrn(p, (u_char *) ngx_argv[i],
0116                             ngx_os_argv_last - (char *) p);
0117             p = ngx_cpystrn(p, (u_char *) " ", ngx_os_argv_last - (char *) p);
0118         }
0119 
0120         if (*(p - 1) == ' ') {
0121             *(p - 1) = ')';
0122         }
0123     }
0124 
0125 #endif
0126 
0127     if (ngx_os_argv_last - (char *) p) {
0128         ngx_memset(p, NGX_SETPROCTITLE_PAD, ngx_os_argv_last - (char *) p);
0129     }
0130 
0131     ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
0132                    "setproctitle: \"%s\"", ngx_os_argv[0]);
0133 }
0134 
0135 #endif /* NGX_SETPROCTITLE_USES_ENV */