From 2f56f8a0943f92d430c8f395880ace8f5922f85b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 25 Jun 2010 21:33:50 +0200 Subject: [PATCH] daemons: drop unnecessary setsid() calls --- avahi-autoipd/main.c | 100 ++++++++++++++++-------------------------- avahi-daemon/chroot.c | 2 - 2 files changed, 39 insertions(+), 63 deletions(-) diff --git a/avahi-autoipd/main.c b/avahi-autoipd/main.c index edf2cd1..4b35f7c 100644 --- a/avahi-autoipd/main.c +++ b/avahi-autoipd/main.c @@ -564,8 +564,7 @@ fail: return -1; } -static void -close_socket(int fd) { +static void close_socket(int fd) { close(fd); } @@ -580,16 +579,14 @@ static uint8_t __lladdr[ETHER_ADDRLEN]; #define elementsof(array) (sizeof(array)/sizeof(array[0])) #endif -static int -__get_ether_addr(int ifindex, u_char *lladdr) -{ - int mib[6]; - char *buf; - struct if_msghdr *ifm; - char *lim; - char *next; - struct sockaddr_dl *sdl; - size_t len; +static int __get_ether_addr(int ifindex, u_char *lladdr) { + int mib[6]; + char *buf; + struct if_msghdr *ifm; + char *lim; + char *next; + struct sockaddr_dl *sdl; + size_t len; mib[0] = CTL_NET; mib[1] = PF_ROUTE; @@ -601,20 +598,15 @@ __get_ether_addr(int ifindex, u_char *lladdr) if (sysctl(mib, elementsof(mib), NULL, &len, NULL, 0) != 0) { daemon_log(LOG_ERR, "sysctl(NET_RT_IFLIST): %s", strerror(errno)); - return (-1); - } - - buf = malloc(len); - if (buf == NULL) { - daemon_log(LOG_ERR, "malloc(%d): %s", len, strerror(errno)); - return (-1); + return -1; } + buf = avahi_malloc(len); if (sysctl(mib, elementsof(mib), buf, &len, NULL, 0) != 0) { daemon_log(LOG_ERR, "sysctl(NET_RT_IFLIST): %s", strerror(errno)); free(buf); - return (-1); + return -1; } lim = buf + len; @@ -625,34 +617,31 @@ __get_ether_addr(int ifindex, u_char *lladdr) memcpy(lladdr, LLADDR(sdl), ETHER_ADDRLEN); } } - free(buf); + avahi_free(buf); - return (0); + return 0; } #define PCAP_TIMEOUT 500 /* 0.5s */ -static int -open_socket(int iface, uint8_t *hw_address) -{ - struct bpf_program bpf; - char *filter; - char ifname[IFNAMSIZ]; - pcap_t *pp; - int err; - int fd; +static int open_socket(int iface, uint8_t *hw_address) { + struct bpf_program bpf; + char *filter; + char ifname[IFNAMSIZ]; + pcap_t *pp; + int err; + int fd; assert(__pp == NULL); - if (interface_up(iface) < 0) { - return (-1); - } - if (__get_ether_addr(iface, __lladdr) == -1) { - return (-1); - } - if (if_indextoname(iface, ifname) == NULL) { - return (-1); - } + if (interface_up(iface) < 0) + return -1; + + if (__get_ether_addr(iface, __lladdr) == -1) + return -1; + + if (if_indextoname(iface, ifname) == NULL) + return -1; /* * Using a timeout for BPF is fairly portable across BSDs. On most @@ -726,10 +715,7 @@ open_socket(int iface, uint8_t *hw_address) return (fd); } -static void -close_socket(int fd __unused) -{ - +static void close_socket(int fd AVAHI_GCC_UNUSED) { assert(__pp != NULL); pcap_close(__pp); __pp = NULL; @@ -739,10 +725,7 @@ close_socket(int fd __unused) * We trick avahi into allocating sizeof(packet) + sizeof(ether_header), * and prepend the required ethernet header information before sending. */ -static int -send_packet(int fd __unused, int iface __unused, ArpPacket *packet, - size_t packet_len) -{ +static int send_packet(int fd AVAHI_GCC_UNUSED, int iface AVAHI_GCC_UNUSED, ArpPacket *packet, size_t packet_len) { struct ether_header *eh; assert(__pp != NULL); @@ -756,14 +739,12 @@ send_packet(int fd __unused, int iface __unused, ArpPacket *packet, return (pcap_inject(__pp, (void *)eh, packet_len + sizeof(*eh))); } -static int -recv_packet(int fd __unused, ArpPacket **packet, size_t *packet_len) -{ - struct pcap_pkthdr *ph; - u_char *pd; - ArpPacket *ap; - int err; - int retval; +static int recv_packet(int fd AVAHI_GCC_UNUSED, ArpPacket **packet, size_t *packet_len) { + struct pcap_pkthdr *ph; + u_char *pd; + ArpPacket *ap; + int err; + int retval; assert(__pp != NULL); assert(packet != NULL); @@ -789,10 +770,9 @@ recv_packet(int fd __unused, ArpPacket **packet, size_t *packet_len) * didn't match the filter, in which case we'll get 0 packets. */ retval = 0; - } else { + } else daemon_log(LOG_ERR, "pcap_next_ex(%d): %s", err, pcap_geterr(__pp)); - } return (retval); } @@ -841,8 +821,6 @@ static FILE* fork_dispatcher(void) { daemon_retval_done(); - setsid(); - avahi_set_proc_title(argv0, "%s: [%s] callout dispatcher", argv0, interface_name); close(fds[1]); @@ -1095,7 +1073,7 @@ static int loop(int iface, uint32_t addr) { char *address_fn = NULL; const char *p; - daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP,0); + daemon_signal_init(SIGINT, SIGTERM, SIGCHLD, SIGHUP, 0); if (!(dispatcher = fork_dispatcher())) goto fail; diff --git a/avahi-daemon/chroot.c b/avahi-daemon/chroot.c index 6b9876b..a418780 100644 --- a/avahi-daemon/chroot.c +++ b/avahi-daemon/chroot.c @@ -307,8 +307,6 @@ int avahi_chroot_helper_start(const char *argv0) { return -1; } else if (pid == 0) { - setsid(); - /* Drop all remaining capabilities */ avahi_caps_drop_all(); -- 2.39.5