--- syslogd.c | 121 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 51 deletions(-) --- syslogd.c +++ syslogd.c 2022-10-17 09:33:43.942759127 +0000 @@ -626,6 +626,9 @@ static volatile sig_atomic_t restart; int nfunix = 1; char *funixn[MAXFUNIX] = { _PATH_LOG }; int funix[MAXFUNIX] = { -1, }; +#if defined(__linux__) +int sd_fds = 0; +#endif #ifdef UT_NAMESIZE # define UNAMESZ UT_NAMESIZE /* length of a login name */ @@ -1047,12 +1050,44 @@ int main(argc, argv) exit(1); } + i = 0; num_fds = getdtablesize(); #if defined(__linux__) - if (sd_listen_fds(0) <= 0) -#endif - for (i = 0; i < num_fds; i++) + /* if (sd_booted()) */ { + const char *e; + char buf[24] = { '\0' }; + char *p = NULL; + unsigned long l; + + /* fork & systemd socket activation: + * fetch listen pid and update to ours, + * when it is set to pid of our parent. + */ + if ( (e = getenv("LISTEN_PID"))) { + errno = 0; + l = strtoul(e, &p, 10); + if (errno == 0 && l > 0 && (!p || !*p)) { + if (getppid() == (pid_t)l) { + snprintf(buf, sizeof(buf), "%d", + getpid()); + setenv("LISTEN_PID", buf, 1); + } + } + } + + /* + * init sd_fds and close all fds, except + * of the fds provided by systemd. + */ + sd_fds = sd_listen_fds(0); + for( ; i < SD_LISTEN_FDS_START; i++) (void) close(i); + if (sd_fds > 0) + i = SD_LISTEN_FDS_START + sd_fds; + } +#endif + for ( ; i < num_fds; i++) + (void) close(i); untty(); break; } @@ -1094,6 +1129,11 @@ int main(argc, argv) exit(1); } } /* if ( !Debug ) */ +#if defined(__linux__) + /* if (sd_booted()) */ { + sd_fds = sd_listen_fds(0); + } +#endif } #endif @@ -1367,54 +1407,30 @@ static int create_unix_socket(const char return -1; #if defined(__linux__) - if (strcmp(path, _PATH_LOG) == 0) { - int r; - - /* Check whether an FD was passed in from systemd. If - * so, it's the /dev/log socket, so use it. */ - - r = sd_listen_fds(0); - if (r < 0) { - logerror("Failed to acquire systemd socket"); + /* + * Check whether an FD was passed in from systemd. + * If so and it matches this socket, so use it. + */ + if (sd_fds < 0) { + logerror("Failed to acquire systemd socket"); #ifndef SYSV - dienow(); + dienow(); #else - return -1; + return -1; #endif - } + } - - if (r > 1) { - logerror("Wrong number of systemd sockets passed"); -#ifndef SYSV - dienow(); -#else - return -1; -#endif - } - - if (r == 1) { - fd = SD_LISTEN_FDS_START; - r = sd_is_socket_unix(fd, SOCK_DGRAM, -1, _PATH_LOG, 0); - if (r < 0) { - logerror("Failed to verify systemd socket type"); -#ifndef SYSV - dienow(); -#else - return -1; -#endif - } - - if (!r) { - logerror("Passed systemd socket of wrong type"); -#ifndef SYSV - dienow(); -#else - return -1; -#endif + if (sd_fds > 0) { + for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + sd_fds; fd++) { + if( sd_is_socket_unix(fd, SOCK_DGRAM, -1, path, 0) == 1) { + /* ok, it matches -- just use as is */ + return fd; } - - return fd; + /* + * otherwise it either didn't matched *this* socket and + * we just continue to check the next one or there were + * an error and we will recreate it bellow. + */ } } #endif @@ -2537,14 +2553,17 @@ void dienow(void) if (InetInuse) close(inetm); /* Clean-up files. */ - i = 0; + for (i = 0; i < nfunix; i++) { + if (funixn[i] && funix[i] != -1) { #if defined(__linux__) - if (sd_listen_fds(0) > 0) - i = 1; + if (sd_fds > 0 && + funix[i] >= SD_LISTEN_FDS_START && + funix[i] < SD_LISTEN_FDS_START + sd_fds) + continue; #endif - for (; i < nfunix; i++) - if (funixn[i] && funix[i] != -1) (void)unlink(funixn[i]); + } + } for (lognum = 0; lognum <= nlogs; lognum++) { f = &Files[lognum];