src/os/unix/ngx_setproctitle.c - nginx-1.7.10

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. #if (NGX_SETPROCTITLE_USES_ENV)

  8. /*
  9. * To change the process title in Linux and Solaris we have to set argv[1]
  10. * to NULL and to copy the title to the same place where the argv[0] points to.
  11. * However, argv[0] may be too small to hold a new title.  Fortunately, Linux
  12. * and Solaris store argv[] and environ[] one after another.  So we should
  13. * ensure that is the continuous memory and then we allocate the new memory
  14. * for environ[] and copy it.  After this we could use the memory starting
  15. * from argv[0] for our process title.
  16. *
  17. * The Solaris's standard /bin/ps does not show the changed process title.
  18. * You have to use "/usr/ucb/ps -w" instead.  Besides, the UCB ps does not
  19. * show a new title if its length less than the origin command line length.
  20. * To avoid it we append to a new title the origin command line in the
  21. * parenthesis.
  22. */

  23. extern char **environ;

  24. static char *ngx_os_argv_last;

  25. ngx_int_t
  26. ngx_init_setproctitle(ngx_log_t *log)
  27. {
  28.     u_char      *p;
  29.     size_t       size;
  30.     ngx_uint_t   i;

  31.     size = 0;

  32.     for (i = 0; environ[i]; i++) {
  33.         size += ngx_strlen(environ[i]) + 1;
  34.     }

  35.     p = ngx_alloc(size, log);
  36.     if (p == NULL) {
  37.         return NGX_ERROR;
  38.     }

  39.     ngx_os_argv_last = ngx_os_argv[0];

  40.     for (i = 0; ngx_os_argv[i]; i++) {
  41.         if (ngx_os_argv_last == ngx_os_argv[i]) {
  42.             ngx_os_argv_last = ngx_os_argv[i] + ngx_strlen(ngx_os_argv[i]) + 1;
  43.         }
  44.     }

  45.     for (i = 0; environ[i]; i++) {
  46.         if (ngx_os_argv_last == environ[i]) {

  47.             size = ngx_strlen(environ[i]) + 1;
  48.             ngx_os_argv_last = environ[i] + size;

  49.             ngx_cpystrn(p, (u_char *) environ[i], size);
  50.             environ[i] = (char *) p;
  51.             p += size;
  52.         }
  53.     }

  54.     ngx_os_argv_last--;

  55.     return NGX_OK;
  56. }


  57. void
  58. ngx_setproctitle(char *title)
  59. {
  60.     u_char     *p;

  61. #if (NGX_SOLARIS)

  62.     ngx_int_t   i;
  63.     size_t      size;

  64. #endif

  65.     ngx_os_argv[1] = NULL;

  66.     p = ngx_cpystrn((u_char *) ngx_os_argv[0], (u_char *) "nginx: ",
  67.                     ngx_os_argv_last - ngx_os_argv[0]);

  68.     p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char *) p);

  69. #if (NGX_SOLARIS)

  70.     size = 0;

  71.     for (i = 0; i < ngx_argc; i++) {
  72.         size += ngx_strlen(ngx_argv[i]) + 1;
  73.     }

  74.     if (size > (size_t) ((char *) p - ngx_os_argv[0])) {

  75.         /*
  76.          * ngx_setproctitle() is too rare operation so we use
  77.          * the non-optimized copies
  78.          */

  79.         p = ngx_cpystrn(p, (u_char *) " (", ngx_os_argv_last - (char *) p);

  80.         for (i = 0; i < ngx_argc; i++) {
  81.             p = ngx_cpystrn(p, (u_char *) ngx_argv[i],
  82.                             ngx_os_argv_last - (char *) p);
  83.             p = ngx_cpystrn(p, (u_char *) " ", ngx_os_argv_last - (char *) p);
  84.         }

  85.         if (*(p - 1) == ' ') {
  86.             *(p - 1) = ')';
  87.         }
  88.     }

  89. #endif

  90.     if (ngx_os_argv_last - (char *) p) {
  91.         ngx_memset(p, NGX_SETPROCTITLE_PAD, ngx_os_argv_last - (char *) p);
  92.     }

  93.     ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
  94.                    "setproctitle: \"%s\"", ngx_os_argv[0]);
  95. }

  96. #endif /* NGX_SETPROCTITLE_USES_ENV */