4 This file is part of avahi.
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.
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.
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
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
36 #include <avahi-common/malloc.h>
38 #include "setproctitle.h"
40 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
41 static char** argv_buffer = NULL;
42 static size_t argv_size = 0;
44 #if !HAVE_DECL_ENVIRON
45 extern char **environ;
50 void avahi_init_proc_title(int argc, char **argv) {
52 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
55 char **new_environ, *endptr;
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 */
61 for (i = 0; environ[i]; i++);
63 endptr = i ? environ[i-1] + strlen(environ[i-1]) : argv[argc-1] + strlen(argv[argc-1]);
66 argv_size = endptr - argv_buffer[0];
68 /* Make a copy of environ */
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;
75 environ = new_environ;
80 void avahi_set_proc_title(const char *name, const char *fmt,...) {
81 #ifdef HAVE_SETPROCTITLE
86 vsnprintf(t, sizeof(t), fmt, ap);
89 setproctitle("-%s", t);
98 vsnprintf(argv_buffer[0], argv_size, fmt, ap);
101 l = strlen(argv_buffer[0]);
103 memset(argv_buffer[0] + l, 0, argv_size - l);
104 argv_buffer[1] = NULL;
107 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
110 prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0);