Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / os / unix / ngx_setproctitle.c
blob91afa5191427c4e7b9caa7ab052f8075cd4072c5
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
12 #if (NGX_SETPROCTITLE_USES_ENV)
15 * To change the process title in Linux and Solaris we have to set argv[1]
16 * to NULL and to copy the title to the same place where the argv[0] points to.
17 * However, argv[0] may be too small to hold a new title. Fortunately, Linux
18 * and Solaris store argv[] and environ[] one after another. So we should
19 * ensure that is the continuous memory and then we allocate the new memory
20 * for environ[] and copy it. After this we could use the memory starting
21 * from argv[0] for our process title.
23 * The Solaris's standard /bin/ps does not show the changed process title.
24 * You have to use "/usr/ucb/ps -w" instead. Besides, the UCB ps does not
25 * show a new title if its length less than the origin command line length.
26 * To avoid it we append to a new title the origin command line in the
27 * parenthesis.
30 extern char **environ;
32 static char *ngx_os_argv_last;
34 ngx_int_t
35 ngx_init_setproctitle(ngx_log_t *log)
37 u_char *p;
38 size_t size;
39 ngx_uint_t i;
41 size = 0;
43 for (i = 0; environ[i]; i++) {
44 size += ngx_strlen(environ[i]) + 1;
47 p = ngx_alloc(size, log);
48 if (p == NULL) {
49 return NGX_ERROR;
52 ngx_os_argv_last = ngx_os_argv[0];
54 for (i = 0; ngx_os_argv[i]; i++) {
55 if (ngx_os_argv_last == ngx_os_argv[i]) {
56 ngx_os_argv_last = ngx_os_argv[i] + ngx_strlen(ngx_os_argv[i]) + 1;
60 for (i = 0; environ[i]; i++) {
61 if (ngx_os_argv_last == environ[i]) {
63 size = ngx_strlen(environ[i]) + 1;
64 ngx_os_argv_last = environ[i] + size;
66 ngx_cpystrn(p, (u_char *) environ[i], size);
67 environ[i] = (char *) p;
68 p += size;
72 ngx_os_argv_last--;
74 return NGX_OK;
78 void
79 ngx_setproctitle(char *title)
81 u_char *p;
83 #if (NGX_SOLARIS)
85 ngx_int_t i;
86 size_t size;
88 #endif
90 ngx_os_argv[1] = NULL;
92 p = ngx_cpystrn((u_char *) ngx_os_argv[0], (u_char *) "nginx: ",
93 ngx_os_argv_last - ngx_os_argv[0]);
95 p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char *) p);
97 #if (NGX_SOLARIS)
99 size = 0;
101 for (i = 0; i < ngx_argc; i++) {
102 size += ngx_strlen(ngx_argv[i]) + 1;
105 if (size > (size_t) ((char *) p - ngx_os_argv[0])) {
108 * ngx_setproctitle() is too rare operation so we use
109 * the non-optimized copies
112 p = ngx_cpystrn(p, (u_char *) " (", ngx_os_argv_last - (char *) p);
114 for (i = 0; i < ngx_argc; i++) {
115 p = ngx_cpystrn(p, (u_char *) ngx_argv[i],
116 ngx_os_argv_last - (char *) p);
117 p = ngx_cpystrn(p, (u_char *) " ", ngx_os_argv_last - (char *) p);
120 if (*(p - 1) == ' ') {
121 *(p - 1) = ')';
125 #endif
127 if (ngx_os_argv_last - (char *) p) {
128 ngx_memset(p, NGX_SETPROCTITLE_PAD, ngx_os_argv_last - (char *) p);
131 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
132 "setproctitle: \"%s\"", ngx_os_argv[0]);
135 #endif /* NGX_SETPROCTITLE_USES_ENV */