From: Lennart Poettering Date: Thu, 31 Aug 2006 18:32:27 +0000 (+0000) Subject: avahi_set_proc_title(): change the process name with prctl() if available X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=9d709a294b81066a0942a7a642df1b3d4940ad26;p=catta avahi_set_proc_title(): change the process name with prctl() if available git-svn-id: file:///home/lennart/svn/public/avahi/trunk@1300 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-autoipd/main.c b/avahi-autoipd/main.c index 1b3c9ed..6d83d3a 100644 --- a/avahi-autoipd/main.c +++ b/avahi-autoipd/main.c @@ -267,13 +267,13 @@ static void set_state(State st, int reset_counter, uint32_t address) { if (modify_proc_title) { if (state == STATE_SLEEPING) - avahi_set_proc_title("%s: sleeping", argv0); + avahi_set_proc_title(argv0, "%s(%s): sleeping", argv0, interface_name); else if (state == STATE_ANNOUNCING) - avahi_set_proc_title("%s: announcing %s", argv0, inet_ntop(AF_INET, &address, buf, sizeof(buf))); + avahi_set_proc_title(argv0, "%s(%s): announcing %s", argv0, interface_name, inet_ntop(AF_INET, &address, buf, sizeof(buf))); else if (state == STATE_RUNNING) - avahi_set_proc_title("%s: bound %s", argv0, inet_ntop(AF_INET, &address, buf, sizeof(buf))); + avahi_set_proc_title(argv0, "%s(%s): bound %s", argv0, interface_name, inet_ntop(AF_INET, &address, buf, sizeof(buf))); else - avahi_set_proc_title("%s: probing %s", argv0, inet_ntop(AF_INET, &address, buf, sizeof(buf))); + avahi_set_proc_title(argv0, "%s(%s): probing %s", argv0, interface_name, inet_ntop(AF_INET, &address, buf, sizeof(buf))); } } @@ -840,7 +840,7 @@ static int parse_command_line(int argc, char *argv[]) { }; opterr = 0; - while ((c = getopt_long(argc, argv, "hDkVrcS:", long_options, NULL)) >= 0) { + while ((c = getopt_long(argc, argv, "hDskrcVS:w", long_options, NULL)) >= 0) { switch(c) { case 's': @@ -903,7 +903,7 @@ static int parse_command_line(int argc, char *argv[]) { return -1; } - interface_name = argv[optind++]; + interface_name = avahi_strdup(argv[optind++]); } if (optind != argc) { @@ -921,6 +921,7 @@ static const char* pid_file_proc(void) { int main(int argc, char*argv[]) { int r = 1; int wrote_pid_file = 0; + char *log_ident = NULL; avahi_init_proc_title(argc, argv); @@ -929,12 +930,15 @@ int main(int argc, char*argv[]) { else argv0 = argv[0]; - daemon_pid_file_ident = daemon_log_ident = argv0; - daemon_pid_file_proc = pid_file_proc; + argv0 = avahi_strdup(argv0); + + daemon_log_ident = argv0; if (parse_command_line(argc, argv) < 0) goto finish; + daemon_log_ident = log_ident = avahi_strdup_printf("%s(%s)", argv0, interface_name); + daemon_pid_file_proc = pid_file_proc; pid_file_name = avahi_strdup_printf(AVAHI_RUNTIME_DIR"/avahi-autoipd.%s.pid", interface_name); if (command == DAEMON_RUN) { @@ -993,6 +997,8 @@ int main(int argc, char*argv[]) { } else wrote_pid_file = 1; + avahi_set_proc_title(argv0, "%s(%s): starting up", argv0, interface_name); + if (loop(ifindex, start_address) < 0) goto finish; @@ -1031,6 +1037,11 @@ finish: if (wrote_pid_file) daemon_pid_file_remove(); + avahi_free(log_ident); + avahi_free(pid_file_name); + avahi_free(argv0); + avahi_free(interface_name); + return r; } diff --git a/avahi-daemon/chroot.c b/avahi-daemon/chroot.c index 5ca605d..99f52fa 100644 --- a/avahi-daemon/chroot.c +++ b/avahi-daemon/chroot.c @@ -314,7 +314,7 @@ int avahi_chroot_helper_start(const char *argv0) { /* Drop all remaining capabilities */ avahi_caps_drop_all(); - avahi_set_proc_title("%s: chroot helper process", argv0); + avahi_set_proc_title(argv0, "%s: chroot helper process", argv0); close(sock[0]); helper_main(sock[1]); diff --git a/avahi-daemon/main.c b/avahi-daemon/main.c index b230ff7..6d8abb1 100644 --- a/avahi-daemon/main.c +++ b/avahi-daemon/main.c @@ -267,7 +267,7 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void *userda case AVAHI_SERVER_RUNNING: avahi_log_info("Server startup complete. Host name is %s. Local service cookie is %u.", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s)); - avahi_set_proc_title("%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s)); + avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s)); static_service_add_to_server(); static_hosts_add_to_server(); @@ -286,7 +286,7 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void *userda case AVAHI_SERVER_COLLISION: { char *n; - avahi_set_proc_title("%s: collision", argv0); + avahi_set_proc_title(argv0, "%s: collision", argv0); static_service_remove_from_server(); static_hosts_remove_from_server(); @@ -308,7 +308,7 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void *userda case AVAHI_SERVER_REGISTERING: - avahi_set_proc_title("%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s)); + avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s)); static_service_remove_from_server(); static_hosts_remove_from_server(); @@ -1193,7 +1193,7 @@ int main(int argc, char *argv[]) { #endif avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0); - avahi_set_proc_title("%s: starting up", argv0); + avahi_set_proc_title(argv0, "%s: starting up", argv0); if (run_server(&config) == 0) r = 0; diff --git a/avahi-daemon/setproctitle.c b/avahi-daemon/setproctitle.c index f8d3ddb..09b7f65 100644 --- a/avahi-daemon/setproctitle.c +++ b/avahi-daemon/setproctitle.c @@ -29,6 +29,10 @@ #include #include +#ifdef HAVE_SYS_PRCTL_H +#include +#endif + #include #include "setproctitle.h" @@ -73,7 +77,7 @@ void avahi_init_proc_title(int argc, char **argv) { #endif } -void avahi_set_proc_title(const char *fmt,...) { +void avahi_set_proc_title(const char *name, const char *fmt,...) { #ifdef HAVE_SETPROCTITLE char t[256]; @@ -99,4 +103,11 @@ void avahi_set_proc_title(const char *fmt,...) { memset(argv_buffer[0] + l, 0, argv_size - l); argv_buffer[1] = NULL; #endif + +#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME) + + if (name) + prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0); + +#endif } diff --git a/avahi-daemon/setproctitle.h b/avahi-daemon/setproctitle.h index 6ac7612..a8f2a1a 100644 --- a/avahi-daemon/setproctitle.h +++ b/avahi-daemon/setproctitle.h @@ -22,7 +22,9 @@ USA. ***/ +#include + void avahi_init_proc_title(int argc, char **argv); -void avahi_set_proc_title(const char *fmt, ...); +void avahi_set_proc_title(const char *name, const char *fmt, ...) AVAHI_GCC_PRINTF_ATTR23; #endif