]> git.meshlink.io Git - catta/blob - avahi-daemon/setproctitle.c
09b7f65f75b96845f5bcaaab9df5fc548a650dcd
[catta] / avahi-daemon / setproctitle.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <unistd.h>
31
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #endif
35
36 #include <avahi-common/malloc.h>
37
38 #include "setproctitle.h"
39
40 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
41 static char** argv_buffer = NULL;
42 static size_t argv_size = 0;
43
44 #if !HAVE_DECL_ENVIRON
45 extern char **environ;
46 #endif
47
48 #endif
49
50 void avahi_init_proc_title(int argc, char **argv) {
51
52 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
53
54     unsigned i;
55     char **new_environ, *endptr;
56
57     /* This code is really really ugly. We make some memory layout
58      * assumptions and reuse the environment array as memory to store
59      * our process title in */
60
61     for (i = 0; environ[i]; i++);
62
63     endptr = i ? environ[i-1] + strlen(environ[i-1]) : argv[argc-1] + strlen(argv[argc-1]);
64
65     argv_buffer = argv;
66     argv_size = endptr - argv_buffer[0];
67
68     /* Make a copy of environ */
69     
70     new_environ = avahi_malloc(sizeof(char*) * (i + 1));
71     for (i = 0; environ[i]; i++)
72         new_environ[i] = avahi_strdup(environ[i]);
73     new_environ[i] = NULL;
74
75     environ = new_environ;
76
77 #endif
78 }       
79
80 void avahi_set_proc_title(const char *name, const char *fmt,...) {
81 #ifdef HAVE_SETPROCTITLE
82     char t[256];
83
84     va_list ap;
85     va_start(ap, fmt);
86     vsnprintf(t, sizeof(t), fmt, ap);
87     va_end(ap);
88
89     setproctitle("-%s", t);
90 #elif __linux__
91     size_t l;
92     va_list ap;
93     
94     if (!argv_buffer)
95         return;
96     
97     va_start(ap, fmt);
98     vsnprintf(argv_buffer[0], argv_size, fmt, ap);
99     va_end(ap);
100
101     l = strlen(argv_buffer[0]);
102     
103     memset(argv_buffer[0] + l, 0, argv_size - l);
104     argv_buffer[1] = NULL;
105 #endif
106
107 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
108
109     if (name)
110         prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0);
111     
112 #endif
113 }