1 Based on http://ftp.free.org/mirrors/rsync.frugalware.org/frugalware-testing/source/apps-extra/sysklogd/sysklogd-1.5-systemd.diff
3 diff -ruN -x '*~' sysklogd-1.5-old/Makefile sysklogd-1.5/Makefile
4 --- sysklogd-1.5-old/Makefile 2007-05-30 17:28:48.000000000 +0200
5 +++ sysklogd-1.5/Makefile 2013-05-09 16:01:14.428638113 +0200
8 #SKFLAGS= -g -DSYSV -Wall
10 -SKFLAGS= $(RPM_OPT_FLAGS) -O3 -DSYSV -fomit-frame-pointer -Wall -fno-strength-reduce
11 +SKFLAGS= $(RPM_OPT_FLAGS) -O3 -DSYSV -fomit-frame-pointer -Wall -fno-strength-reduce -I.
12 # -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
13 # -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE
14 # $(shell getconf LFS_SKFLAGS)
17 install: install_man install_exec
19 -syslogd: syslogd.o pidfile.o
20 - ${CC} ${LDFLAGS} -o syslogd syslogd.o pidfile.o ${LIBS}
21 +syslogd: syslogd.o pidfile.o sd-daemon.o
22 + ${CC} ${LDFLAGS} -o syslogd syslogd.o pidfile.o sd-daemon.o ${LIBS}
24 klogd: klogd.o syslog.o pidfile.o ksym.o ksym_mod.o
25 ${CC} ${LDFLAGS} -o klogd klogd.o syslog.o pidfile.o ksym.o \
28 ${CC} ${SKFLAGS} ${SYSLOG_FLAGS} -c syslog.c
30 +sd-daemon.o: sd-daemon.c sd-daemon.h
31 + ${CC} ${SKFLAGS} ${SYSLOG_FLAGS} -c sd-daemon.c
33 klogd.o: klogd.c klogd.h version.h
34 ${CC} ${SKFLAGS} ${KLOGD_FLAGS} $(DEB) -c klogd.c
36 diff -ruN -x '*~' sysklogd-1.5-old/sd-daemon.c sysklogd-1.5/sd-daemon.c
37 --- sysklogd-1.5-old/sd-daemon.c 1970-01-01 01:00:00.000000000 +0100
38 +++ sysklogd-1.5/sd-daemon.c 2013-05-09 16:01:14.429638107 +0200
40 +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
43 + Copyright 2010 Lennart Poettering
45 + Permission is hereby granted, free of charge, to any person
46 + obtaining a copy of this software and associated documentation files
47 + (the "Software"), to deal in the Software without restriction,
48 + including without limitation the rights to use, copy, modify, merge,
49 + publish, distribute, sublicense, and/or sell copies of the Software,
50 + and to permit persons to whom the Software is furnished to do so,
51 + subject to the following conditions:
53 + The above copyright notice and this permission notice shall be
54 + included in all copies or substantial portions of the Software.
56 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
57 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
59 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
60 + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
61 + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
62 + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
70 +#include <sys/types.h>
71 +#include <sys/stat.h>
72 +#include <sys/socket.h>
74 +#include <netinet/in.h>
84 +#include "sd-daemon.h"
86 +int sd_listen_fds(int unset_environment) {
88 +#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
96 + if (!(e = getenv("LISTEN_PID"))) {
102 + l = strtoul(e, &p, 10);
109 + if (!p || *p || l <= 0) {
114 + /* Is this for us? */
115 + if (getpid() != (pid_t) l) {
120 + if (!(e = getenv("LISTEN_FDS"))) {
126 + l = strtoul(e, &p, 10);
138 + for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + (int) l; fd ++) {
141 + if ((flags = fcntl(fd, F_GETFD)) < 0) {
146 + if (flags & FD_CLOEXEC)
149 + if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
158 + if (unset_environment) {
159 + unsetenv("LISTEN_PID");
160 + unsetenv("LISTEN_FDS");
167 +int sd_is_fifo(int fd, const char *path) {
173 + memset(&st_fd, 0, sizeof(st_fd));
174 + if (fstat(fd, &st_fd) < 0)
177 + if (!S_ISFIFO(st_fd.st_mode))
181 + struct stat st_path;
183 + memset(&st_path, 0, sizeof(st_path));
184 + if (stat(path, &st_path) < 0) {
186 + if (errno == ENOENT || errno == ENOTDIR)
193 + st_path.st_dev == st_fd.st_dev &&
194 + st_path.st_ino == st_fd.st_ino;
200 +static int sd_is_socket_internal(int fd, int type, int listening) {
203 + if (fd < 0 || type < 0)
206 + if (fstat(fd, &st_fd) < 0)
209 + if (!S_ISSOCK(st_fd.st_mode))
213 + int other_type = 0;
214 + socklen_t l = sizeof(other_type);
216 + if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &other_type, &l) < 0)
219 + if (l != sizeof(other_type))
222 + if (other_type != type)
226 + if (listening >= 0) {
228 + socklen_t l = sizeof(accepting);
230 + if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &accepting, &l) < 0)
233 + if (l != sizeof(accepting))
236 + if (!accepting != !listening)
243 +union sockaddr_union {
244 + struct sockaddr sa;
245 + struct sockaddr_in in4;
246 + struct sockaddr_in6 in6;
247 + struct sockaddr_un un;
248 + struct sockaddr_storage storage;
251 +int sd_is_socket(int fd, int family, int type, int listening) {
257 + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
261 + union sockaddr_union sockaddr;
264 + memset(&sockaddr, 0, sizeof(sockaddr));
265 + l = sizeof(sockaddr);
267 + if (getsockname(fd, &sockaddr.sa, &l) < 0)
270 + if (l < sizeof(sa_family_t))
273 + return sockaddr.sa.sa_family == family;
279 +int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) {
280 + union sockaddr_union sockaddr;
284 + if (family != 0 && family != AF_INET && family != AF_INET6)
287 + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
290 + memset(&sockaddr, 0, sizeof(sockaddr));
291 + l = sizeof(sockaddr);
293 + if (getsockname(fd, &sockaddr.sa, &l) < 0)
296 + if (l < sizeof(sa_family_t))
299 + if (sockaddr.sa.sa_family != AF_INET &&
300 + sockaddr.sa.sa_family != AF_INET6)
304 + if (sockaddr.sa.sa_family != family)
308 + if (sockaddr.sa.sa_family == AF_INET) {
309 + if (l < sizeof(struct sockaddr_in))
312 + return htons(port) == sockaddr.in4.sin_port;
314 + if (l < sizeof(struct sockaddr_in6))
317 + return htons(port) == sockaddr.in6.sin6_port;
324 +int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) {
325 + union sockaddr_union sockaddr;
329 + if ((r = sd_is_socket_internal(fd, type, listening)) <= 0)
332 + memset(&sockaddr, 0, sizeof(sockaddr));
333 + l = sizeof(sockaddr);
335 + if (getsockname(fd, &sockaddr.sa, &l) < 0)
338 + if (l < sizeof(sa_family_t))
341 + if (sockaddr.sa.sa_family != AF_UNIX)
346 + length = strlen(path);
349 + /* Unnamed socket */
350 + return l == offsetof(struct sockaddr_un, sun_path);
353 + /* Normal path socket */
355 + (l >= offsetof(struct sockaddr_un, sun_path) + length + 1) &&
356 + memcmp(path, sockaddr.un.sun_path, length+1) == 0;
358 + /* Abstract namespace socket */
360 + (l == offsetof(struct sockaddr_un, sun_path) + length) &&
361 + memcmp(path, sockaddr.un.sun_path, length) == 0;
367 +int sd_notify(int unset_environment, const char *state) {
368 +#if defined(DISABLE_SYSTEMD) || !defined(__linux__) || !defined(SOCK_CLOEXEC)
372 + struct msghdr msghdr;
373 + struct iovec iovec;
374 + union sockaddr_union sockaddr;
382 + if (!(e = getenv("NOTIFY_SOCKET")))
385 + /* Must be an abstract socket, or an absolute path */
386 + if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
391 + if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
396 + memset(&sockaddr, 0, sizeof(sockaddr));
397 + sockaddr.sa.sa_family = AF_UNIX;
398 + strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
400 + if (sockaddr.un.sun_path[0] == '@')
401 + sockaddr.un.sun_path[0] = 0;
403 + memset(&iovec, 0, sizeof(iovec));
404 + iovec.iov_base = (char*) state;
405 + iovec.iov_len = strlen(state);
407 + memset(&msghdr, 0, sizeof(msghdr));
408 + msghdr.msg_name = &sockaddr;
409 + msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(e);
411 + if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
412 + msghdr.msg_namelen = sizeof(struct sockaddr_un);
414 + msghdr.msg_iov = &iovec;
415 + msghdr.msg_iovlen = 1;
417 + if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
425 + if (unset_environment)
426 + unsetenv("NOTIFY_SOCKET");
435 +int sd_notifyf(int unset_environment, const char *format, ...) {
436 +#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
443 + va_start(ap, format);
444 + r = vasprintf(&p, format, ap);
450 + r = sd_notify(unset_environment, p);
457 +int sd_booted(void) {
458 +#if defined(DISABLE_SYSTEMD) || !defined(__linux__)
464 + /* We simply test whether the systemd cgroup hierarchy is
467 + if (lstat("/sys/fs/cgroup", &a) < 0)
470 + if (lstat("/sys/fs/cgroup/systemd", &b) < 0)
473 + return a.st_dev != b.st_dev;
476 diff -ruN -x '*~' sysklogd-1.5-old/sd-daemon.h sysklogd-1.5/sd-daemon.h
477 --- sysklogd-1.5-old/sd-daemon.h 1970-01-01 01:00:00.000000000 +0100
478 +++ sysklogd-1.5/sd-daemon.h 2013-05-09 16:01:14.429638107 +0200
480 +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
482 +#ifndef foosddaemonhfoo
483 +#define foosddaemonhfoo
486 + Copyright 2010 Lennart Poettering
488 + Permission is hereby granted, free of charge, to any person
489 + obtaining a copy of this software and associated documentation files
490 + (the "Software"), to deal in the Software without restriction,
491 + including without limitation the rights to use, copy, modify, merge,
492 + publish, distribute, sublicense, and/or sell copies of the Software,
493 + and to permit persons to whom the Software is furnished to do so,
494 + subject to the following conditions:
496 + The above copyright notice and this permission notice shall be
497 + included in all copies or substantial portions of the Software.
499 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
500 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
501 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
502 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
503 + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
504 + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
505 + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
509 +#include <sys/types.h>
510 +#include <inttypes.h>
517 + Reference implementation of a few systemd related interfaces for
518 + writing daemons. These interfaces are trivial to implement. To
519 + simplify porting we provide this reference implementation.
520 + Applications are welcome to reimplement the algorithms described
521 + here if they do not want to include these two source files.
523 + The following functionality is provided:
525 + - Support for logging with log levels on stderr
526 + - File descriptor passing for socket-based activation
527 + - Daemon startup and status notification
528 + - Detection of systemd boots
530 + You may compile this with -DDISABLE_SYSTEMD to disable systemd
531 + support. This makes all those calls NOPs that are directly related to
532 + systemd (i.e. only sd_is_xxx() will stay useful).
534 + Since this is drop-in code we don't want any of our symbols to be
535 + exported in any case. Hence we declare hidden visibility for all of
538 + You may find an up-to-date version of these source files online:
540 + http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h
541 + http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c
543 + This should compile on non-Linux systems, too, but with the
544 + exception of the sd_is_xxx() calls all functions will become NOPs.
546 + See sd-daemon(7) for more information.
549 +#ifndef _sd_printf_attr_
551 +#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
553 +#define _sd_printf_attr_(a,b)
558 +#if (__GNUC__ >= 4) && !defined(SD_EXPORT_SYMBOLS)
559 +#define _sd_hidden_ __attribute__ ((visibility("hidden")))
566 + Log levels for usage on stderr:
568 + fprintf(stderr, SD_NOTICE "Hello World!\n");
570 + This is similar to printk() usage in the kernel.
572 +#define SD_EMERG "<0>" /* system is unusable */
573 +#define SD_ALERT "<1>" /* action must be taken immediately */
574 +#define SD_CRIT "<2>" /* critical conditions */
575 +#define SD_ERR "<3>" /* error conditions */
576 +#define SD_WARNING "<4>" /* warning conditions */
577 +#define SD_NOTICE "<5>" /* normal but significant condition */
578 +#define SD_INFO "<6>" /* informational */
579 +#define SD_DEBUG "<7>" /* debug-level messages */
581 +/* The first passed file descriptor is fd 3 */
582 +#define SD_LISTEN_FDS_START 3
585 + Returns how many file descriptors have been passed, or a negative
586 + errno code on failure. Optionally, removes the $LISTEN_FDS and
587 + $LISTEN_PID file descriptors from the environment (recommended, but
588 + problematic in threaded environments). If r is the return value of
589 + this function you'll find the file descriptors passed as fds
590 + SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
591 + errno style error code on failure. This function call ensures that
592 + the FD_CLOEXEC flag is set for the passed file descriptors, to make
593 + sure they are not passed on to child processes. If FD_CLOEXEC shall
594 + not be set, the caller needs to unset it after this call for all file
595 + descriptors that are used.
597 + See sd_listen_fds(3) for more information.
599 +int sd_listen_fds(int unset_environment) _sd_hidden_;
602 + Helper call for identifying a passed file descriptor. Returns 1 if
603 + the file descriptor is a FIFO in the file system stored under the
604 + specified path, 0 otherwise. If path is NULL a path name check will
605 + not be done and the call only verifies if the file descriptor
606 + refers to a FIFO. Returns a negative errno style error code on
609 + See sd_is_fifo(3) for more information.
611 +int sd_is_fifo(int fd, const char *path) _sd_hidden_;
614 + Helper call for identifying a passed file descriptor. Returns 1 if
615 + the file descriptor is a socket of the specified family (AF_INET,
616 + ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
617 + family is 0 a socket family check will not be done. If type is 0 a
618 + socket type check will not be done and the call only verifies if
619 + the file descriptor refers to a socket. If listening is > 0 it is
620 + verified that the socket is in listening mode. (i.e. listen() has
621 + been called) If listening is == 0 it is verified that the socket is
622 + not in listening mode. If listening is < 0 no listening mode check
623 + is done. Returns a negative errno style error code on failure.
625 + See sd_is_socket(3) for more information.
627 +int sd_is_socket(int fd, int family, int type, int listening) _sd_hidden_;
630 + Helper call for identifying a passed file descriptor. Returns 1 if
631 + the file descriptor is an Internet socket, of the specified family
632 + (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
633 + SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
634 + check is not done. If type is 0 a socket type check will not be
635 + done. If port is 0 a socket port check will not be done. The
636 + listening flag is used the same way as in sd_is_socket(). Returns a
637 + negative errno style error code on failure.
639 + See sd_is_socket_inet(3) for more information.
641 +int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) _sd_hidden_;
644 + Helper call for identifying a passed file descriptor. Returns 1 if
645 + the file descriptor is an AF_UNIX socket of the specified type
646 + (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
647 + a socket type check will not be done. If path is NULL a socket path
648 + check will not be done. For normal AF_UNIX sockets set length to
649 + 0. For abstract namespace sockets set length to the length of the
650 + socket name (including the initial 0 byte), and pass the full
651 + socket path in path (including the initial 0 byte). The listening
652 + flag is used the same way as in sd_is_socket(). Returns a negative
653 + errno style error code on failure.
655 + See sd_is_socket_unix(3) for more information.
657 +int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) _sd_hidden_;
660 + Informs systemd about changed daemon state. This takes a number of
661 + newline separated environment-style variable assignments in a
662 + string. The following variables are known:
664 + READY=1 Tells systemd that daemon startup is finished (only
665 + relevant for services of Type=notify). The passed
666 + argument is a boolean "1" or "0". Since there is
667 + little value in signalling non-readiness the only
668 + value daemons should send is "READY=1".
670 + STATUS=... Passes a single-line status string back to systemd
671 + that describes the daemon state. This is free-from
672 + and can be used for various purposes: general state
673 + feedback, fsck-like programs could pass completion
674 + percentages and failing programs could pass a human
675 + readable error message. Example: "STATUS=Completed
676 + 66% of file system check..."
678 + ERRNO=... If a daemon fails, the errno-style error code,
679 + formatted as string. Example: "ERRNO=2" for ENOENT.
681 + BUSERROR=... If a daemon fails, the D-Bus error-style error
682 + code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
684 + MAINPID=... The main pid of a daemon, in case systemd did not
685 + fork off the process itself. Example: "MAINPID=4711"
687 + Daemons can choose to send additional variables. However, it is
688 + recommened to prefix variable names not listed above with X_.
690 + Returns a negative errno-style error code on failure. Returns > 0
691 + if systemd could be notified, 0 if it couldn't possibly because
692 + systemd is not running.
694 + Example: When a daemon finished starting up, it could issue this
695 + call to notify systemd about it:
697 + sd_notify(0, "READY=1");
699 + See sd_notifyf() for more complete examples.
701 + See sd_notify(3) for more information.
703 +int sd_notify(int unset_environment, const char *state) _sd_hidden_;
706 + Similar to sd_notify() but takes a format string.
708 + Example 1: A daemon could send the following after initialization:
710 + sd_notifyf(0, "READY=1\n"
711 + "STATUS=Processing requests...\n"
713 + (unsigned long) getpid());
715 + Example 2: A daemon could send the following shortly before
716 + exiting, on failure:
718 + sd_notifyf(0, "STATUS=Failed to start up: %s\n"
723 + See sd_notifyf(3) for more information.
725 +int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3) _sd_hidden_;
728 + Returns > 0 if the system was booted with systemd. Returns < 0 on
729 + error. Returns 0 if the system was not booted with systemd. Note
730 + that all of the functions above handle non-systemd boots just
731 + fine. You should NOT protect them with a call to this function. Also
732 + note that this function checks whether the system, not the user
733 + session is controlled by systemd. However the functions above work
734 + for both user and system services.
736 + See sd_booted(3) for more information.
738 +int sd_booted(void) _sd_hidden_;
745 diff -ruN -x '*~' sysklogd-1.5-old/syslogd.c sysklogd-1.5/syslogd.c
746 --- sysklogd-1.5-old/syslogd.c 2007-07-04 21:04:01.000000000 +0200
747 +++ sysklogd-1.5/syslogd.c 2013-05-09 16:04:32.106602589 +0200
750 #if defined(__linux__)
752 +#include <sd-daemon.h>
758 signal (SIGTERM, SIG_DFL);
759 num_fds = getdtablesize();
760 - for (i= 0; i < num_fds; i++)
762 +#if defined(__linux__)
763 + if (sd_listen_fds(0) <= 0)
765 + for (i = 0; i < num_fds; i++)
770 @@ -1253,6 +1257,60 @@
774 +#if defined(__linux__)
775 + if (strcmp(path, _PATH_LOG) == 0) {
778 + /* Check whether an FD was passed in from systemd. If
779 + * so, it's the /dev/log socket, so use it. */
781 + r = sd_listen_fds(0);
783 + logerror("Failed to acquire systemd socket");
793 + logerror("Wrong number of systemd sockets passed");
802 + fd = SD_LISTEN_FDS_START;
803 + r = sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/syslog", 0);
805 + logerror("Failed to verify systemd socket type");
814 + logerror("Passed systemd socket of wrong type");
822 + dprintf("Using systemd socket (%d).\n", fd);
830 memset(&sunx, 0, sizeof(sunx));
831 @@ -2254,9 +2312,11 @@
832 if (InetInuse) close(inetm);
834 /* Clean-up files. */
835 - for (i = 0; i < nfunix; i++)
836 - if (funixn[i] && funix[i] != -1)
837 - (void)unlink(funixn[i]);
839 +#if defined(__linux__)
840 + if (sd_listen_fds(0) > 0)
844 (void) remove_pid(PidFile);