2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #ifdef HAVE_SYS_PRCTL_H
31 #include <sys/prctl.h>
34 #include <avahi-common/malloc.h>
36 #include "setproctitle.h"
38 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
39 static char** argv_buffer = NULL;
40 static size_t argv_size = 0;
42 #if !HAVE_DECL_ENVIRON
43 extern char **environ;
48 void avahi_init_proc_title(int argc, char **argv) {
50 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
53 char **new_environ, *endptr;
55 /* This code is really really ugly. We make some memory layout
56 * assumptions and reuse the environment array as memory to store
57 * our process title in */
59 for (i = 0; environ[i]; i++);
61 endptr = i ? environ[i-1] + strlen(environ[i-1]) : argv[argc-1] + strlen(argv[argc-1]);
64 argv_size = endptr - argv_buffer[0];
66 /* Make a copy of environ */
68 new_environ = avahi_malloc(sizeof(char*) * (i + 1));
69 for (i = 0; environ[i]; i++)
70 new_environ[i] = avahi_strdup(environ[i]);
71 new_environ[i] = NULL;
73 environ = new_environ;
78 void avahi_set_proc_title(const char *name, const char *fmt,...) {
79 #ifdef HAVE_SETPROCTITLE
84 vsnprintf(t, sizeof(t), fmt, ap);
87 setproctitle("-%s", t);
96 vsnprintf(argv_buffer[0], argv_size, fmt, ap);
99 l = strlen(argv_buffer[0]);
101 memset(argv_buffer[0] + l, 0, argv_size - l);
102 argv_buffer[1] = NULL;
105 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
108 prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0);