Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/process-title.c @ 9403:cf291558ce28 HEAD
process_title_set(): Don't include executable name in setproctitle(), it's done internally.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 30 Sep 2009 17:07:12 -0400 |
parents | b9faf4db2a9f |
children | 00cd9aacd03c |
rev | line source |
---|---|
8590
b9faf4db2a9f
Updated copyright notices to include year 2009.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1 /* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */ |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
1741
9df02b1533b3
Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents:
894
diff
changeset
|
3 /* |
9df02b1533b3
Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents:
894
diff
changeset
|
4 LINUX_PROCTITLE_HACK code from: |
9df02b1533b3
Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents:
894
diff
changeset
|
5 http://lightconsulting.com/~thalakan/process-title-notes.html |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 */ |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 #include "lib.h" |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 #include "process-title.h" |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 |
894
efe7c015dd78
Include unistd.h for setproctitle(), FreeBSD wants it.
Timo Sirainen <tss@iki.fi>
parents:
864
diff
changeset
|
11 #include <stdlib.h> /* NetBSD, OpenBSD */ |
efe7c015dd78
Include unistd.h for setproctitle(), FreeBSD wants it.
Timo Sirainen <tss@iki.fi>
parents:
864
diff
changeset
|
12 #include <unistd.h> /* FreeBSD */ |
864 | 13 |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 /* NOTE: This really is a horrible hack, I don't recommend using it for |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 anything else than debugging. */ |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 /*#define LINUX_PROCTITLE_HACK*/ |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 static char *process_name = NULL; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 #ifdef LINUX_PROCTITLE_HACK |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 static char *process_title; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 static size_t process_title_len; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
24 static void linux_proctitle_init(char *argv[], char *envp[]) |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 { |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 extern char **environ; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 char **p; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 int i; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
30 /* copy environment elsewhere */ |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 for (i = 0; envp[i] != NULL; i++) |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 ; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
33 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
34 if ((p = malloc((i + 1) * sizeof(char *))) == NULL) |
3198
cb285bd5d8c9
If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
35 i_fatal_status(FATAL_OUTOFMEM, "malloc() failed: %m"); |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 environ = p; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 for (i = 0; envp[i] != NULL; i++) { |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
39 if ((environ[i] = strdup(envp[i])) == NULL) |
3198
cb285bd5d8c9
If we run out of memory, exit with FATAL_OUTOFMEM status instead of dumping
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
40 i_fatal_status(FATAL_OUTOFMEM, "strdup() failed: %m"); |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 } |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 environ[i] = NULL; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 /* memory is allocated so that argv[] comes first, environment next. |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
45 Calculate the max. size for process name with by checking the |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
46 address for last environment and it's length. */ |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
47 process_title = argv[0]; |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
48 process_title_len = (size_t) (envp[i-1] - argv[0]) + strlen(envp[i-1]); |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
49 } |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 static void linux_proctitle_set(const char *title) |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 { |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
53 i_strocpy(process_title, title, process_title_len); |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 } |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
55 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 #endif |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 |
9403
cf291558ce28
process_title_set(): Don't include executable name in setproctitle(), it's done internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
58 void process_title_init(char *argv[], char *envp[] ATTR_UNUSED) |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 { |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
60 #ifdef LINUX_PROCTITLE_HACK |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
61 linux_proctitle_init(argv, envp); |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
62 #endif |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
63 process_name = argv[0]; |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
64 } |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
65 |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
3198
diff
changeset
|
66 void process_title_set(const char *title ATTR_UNUSED) |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
67 { |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
68 i_assert(process_name != NULL); |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
69 |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
70 #ifdef HAVE_SETPROCTITLE |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
71 if (title == NULL) |
9403
cf291558ce28
process_title_set(): Don't include executable name in setproctitle(), it's done internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
72 setproctitle(NULL); |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
73 else |
9403
cf291558ce28
process_title_set(): Don't include executable name in setproctitle(), it's done internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
74 setproctitle("%s", title); |
745
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
75 #elif defined(LINUX_PROCTITLE_HACK) |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
76 linux_proctitle_set(t_strconcat(process_name, " ", title, NULL)); |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
77 #endif |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 } |
32040b730b37
Login process now changes it's name if verbose_proctitle = yes, and we're
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
79 |