Home | History | Annotate | Download | only in avahi-daemon
      1 /***
      2   This file is part of avahi.
      3 
      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.
      8 
      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.
     13 
     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
     17   USA.
     18 ***/
     19 
     20 #ifdef HAVE_CONFIG_H
     21 #include <config.h>
     22 #endif
     23 
     24 #include <stdio.h>
     25 #include <stdlib.h>
     26 #include <string.h>
     27 #include <stdarg.h>
     28 #include <unistd.h>
     29 
     30 #ifdef HAVE_SYS_PRCTL_H
     31 #include <sys/prctl.h>
     32 #endif
     33 
     34 #include "avahi-common/avahi-malloc.h"
     35 
     36 #include "setproctitle.h"
     37 
     38 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
     39 static char** argv_buffer = NULL;
     40 static size_t argv_size = 0;
     41 
     42 #if !HAVE_DECL_ENVIRON
     43 extern char **environ;
     44 #endif
     45 
     46 #endif
     47 
     48 void avahi_init_proc_title(int argc, char **argv) {
     49 
     50 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
     51 
     52     unsigned i;
     53     char **new_environ, *endptr;
     54 
     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 */
     58 
     59     for (i = 0; environ[i]; i++);
     60 
     61     endptr = i ? environ[i-1] + strlen(environ[i-1]) : argv[argc-1] + strlen(argv[argc-1]);
     62 
     63     argv_buffer = argv;
     64     argv_size = endptr - argv_buffer[0];
     65 
     66     /* Make a copy of environ */
     67 
     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;
     72 
     73     environ = new_environ;
     74 
     75 #endif
     76 }
     77 
     78 void avahi_set_proc_title(const char *name, const char *fmt,...) {
     79 #ifdef HAVE_SETPROCTITLE
     80     char t[256];
     81 
     82     va_list ap;
     83     va_start(ap, fmt);
     84     vsnprintf(t, sizeof(t), fmt, ap);
     85     va_end(ap);
     86 
     87     setproctitle("-%s", t);
     88 #elif defined(__linux__)
     89     size_t l;
     90     va_list ap;
     91 
     92     if (!argv_buffer)
     93         return;
     94 
     95     va_start(ap, fmt);
     96     vsnprintf(argv_buffer[0], argv_size, fmt, ap);
     97     va_end(ap);
     98 
     99     l = strlen(argv_buffer[0]);
    100 
    101     memset(argv_buffer[0] + l, 0, argv_size - l);
    102     argv_buffer[1] = NULL;
    103 #endif
    104 
    105 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
    106 
    107     if (name)
    108         prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0);
    109 
    110 #endif
    111 }
    112