3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #include <ngx_config.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
30 extern char **environ
;
32 static char *ngx_os_argv_last
;
35 ngx_init_setproctitle(ngx_log_t
*log
)
43 for (i
= 0; environ
[i
]; i
++) {
44 size
+= ngx_strlen(environ
[i
]) + 1;
47 p
= ngx_alloc(size
, log
);
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
;
79 ngx_setproctitle(char *title
)
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
);
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) == ' ') {
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 */