#!/bin/bash # # /lib/systemd/system-generator/syslog-service-generator # # Copyright (c) 2011 SuSE LINUX Products GmbH, Germany. # # Description: # # Write out syslog.service and if required klogd.service depending # on the configuration found in /etc/sysconfig/syslog. Also check # if network target is required with this we do not required the # earlysyslog hack anymore. Based on the /etc/init.d/syslog script # as well as on the /etc/init.d/earlysyslog wrapper script. # # Author: Werner Fink # Please send feedback to http://www.suse.de/feedback # umask 0022 BINDIR=/sbin UNIT_PATH=/etc/systemd/system RUN_PATH=/run/systemd/system test -s /etc/sysconfig/syslog && . /etc/sysconfig/syslog # # Update $SYSLOG_DAEMON if not up-to-date # if test -z "$SYSLOG_DAEMON" -o ! -x "$BINDIR/$SYSLOG_DAEMON" ; then for SYSLOG_DAEMON in rsyslogd syslog-ng syslogd ; do test -x "$BINDIR/$SYSLOG_DAEMON" && break done fi # # Default is no network and start klogd # network= start_klogd=yes klogd=klogd.service # # Now check for the three possible system loggers for required # network connection and for external klogd. Note that /var/run # does not exist at boot time, therefore files and directories # will be created below the /run tmpfs. # case "$SYSLOG_DAEMON" in syslog-ng) syslog=syslog-ng config=/etc/syslog-ng/syslog-ng.conf socksdir=/var/run/syslog-ng socks=$socksdir/additional-log-sockets.conf if test -s "$config" ; then while read line ; do case "$line" in \#*|"") continue ;; *udp\ *|*udp\(*) network=network.target ;; *tcp\ *|*tcp\(*) network=network.target ;; esac done < ${config} fi /bin/mkdir -p -m 0755 ${socksdir#/var} /bin/touch -m 0600 ${socks#/var} params="$SYSLOG_NG_PARAMS" ;; rsyslogd) syslog=rsyslogd config=/etc/rsyslog.early.conf socksdir=/var/run/rsyslog socks=$socksdir/additional-log-sockets.conf # # In hope this works with the rsyslog.early.conf file # (hard to implement for rsyslog with its includes/if # statements)... # if test -s "$config" ; then while read select action ; do case "$select" in \#*|"") continue esac case "$action" in *@*) network=network.target break esac done < ${config} test -n "$network" && config=/etc/rsyslog.conf else config=/etc/rsyslog.conf fi if test -r "$config" ; then while read one two rest ; do if test "$one" = '$ModLoad' -a "$two" = 'imklog.so' ; then start_klogd=no break fi done < $config fi /bin/mkdir -p -m 0755 ${socksdir#/var} /bin/touch -m 0600 ${socks#/var} compat=${RSYSLOGD_COMPAT_VERSION:-${RSYSLOGD_NATIVE_VERSION}} params="-c ${compat:-3} -f $config $RSYSLOGD_PARAMS" ;; *) syslog=syslogd config=/etc/syslog.conf socksdir=/var/run/syslog socks=$socksdir/additional-log-sockets.env if test -s "$config" ; then while read select action ; do case "$select" in \#*|"") continue ;; esac case "$action" in *@*) network=network.target ;; esac done < ${config} fi if test -r "$config" ; then while read line; do case $line in file*/proc/kmsg*) start_klogd=no break esac done < $config fi /bin/mkdir -p -m 0755 ${socksdir#/var} /bin/touch -m 0600 ${socks#/var} params="$SYSLOGD_PARAMS" esac syslog_bin=${BINDIR}/$syslog klog_bin=${BINDIR}/klogd test -s ${config} || exit 1 test -x ${syslog_bin} || exit 1 # # Make sure that we have a log fifo which is used by X11 # if test -e /dev/xconsole -a ! -p /dev/xconsole ; then /bin/rm -f /dev/xconsole fi if test ! -e /dev/xconsole ; then /bin/mknod -m 0600 /dev/xconsole p /bin/chown root:tty /dev/xconsole fi # # No klogd.service required # test "$start_klogd" = no && klogd= # # Create a temporary file for syslog.service # trap 'rm -f "$service"' EXIT service=$(/bin/mktemp $RUN_PATH/syslog.XXXXXXXX) || exit 1 (/bin/cat > $service)<<-EOF # Warning: Do not edit as this service file has been and will be # autogenerated by $0 [Unit] Description=System Logging Service Requires=var-run.mount ${network:+$network }syslog.target${klogd:+ $klogd} After=var-run.mount${network:+ $network} Before=syslog.target${klogd:+ $klogd} OnFailure=systemd-kmsg-syslogd.service [Service] Type=forking Sockets=syslog.socket PIDFile=/var/run/${syslog_bin##*/}.pid StandardOutput=null ExecStartPre=/bin/systemctl stop systemd-kmsg-syslogd.service ExecStartPre=-$socksdir/addsockets EOF # # For additional sockets we use further configuration files # for rsyslogd and syslog-ng but an envrionment file for syslogd # case "$SYSLOG_DAEMON" in rsyslogd) (/bin/cat > ${socksdir#/var}/addsockets)<<-EOF #!/bin/sh # Warning: Do not edit as this file has been and will be # autogenerated by $0 test -s /etc/sysconfig/syslog && . /etc/sysconfig/syslog umask 0022 > $socks for variable in \${!SYSLOGD_ADDITIONAL_SOCKET*}; do eval value=\\\$\$variable test -z "\$value" && continue test ! -d \${value%/*} && continue echo "\\\$AddUnixListenSocket \$value" done >> $socks EOF ;; syslog-ng) (/bin/cat > ${socksdir#/var}/addsockets)<<-EOF #!/bin/sh # Warning: Do not edit as this file has been and will be # autogenerated by $0 test -s /etc/sysconfig/syslog && . /etc/sysconfig/syslog umask 0022 echo 'source chroots {' > $socks for variable in \${!SYSLOGD_ADDITIONAL_SOCKET*}; do eval value=\\\$\$variable test -z "\$value" && continue test ! -d \${value%/*} && continue echo "unix-dgram(\\"\$value\\");" done >> $socks echo '};' >> $socks EOF ;; *) (/bin/cat > ${socksdir#/var}/addsockets)<<-EOF #!/bin/sh # Warning: Do not edit as this file has been and will be # autogenerated by $0 test -s /etc/sysconfig/syslog && . /etc/sysconfig/syslog umask 0022 echo -n 'ADDITIONAL_SOCKET="' > $socks for variable in \${!SYSLOGD_ADDITIONAL_SOCKET*}; do eval value=\\\$\$variable test -z "\$value" && continue test ! -d \${value%/*} && continue echo -n "-a \$value " done >> $socks echo -n '"' >> $socks EOF echo "Environment=ADDITIONAL_SOCKET=" >>$service echo "EnvironmentFile=-$socks" 1>>$service params="${params:+$params }\$ADDITIONAL_SOCKET" esac chmod u+x ${socksdir#/var}/addsockets # # If klogd.service is required handle reload of syslog.service efficient # otherwise we may loose kernel messages or klogd is forwarding kernel # messages back to systemd as the creator of the system logging socket # and we would get a message loop. # exec 3>&1 1>>$service echo "ExecStart=$syslog_bin $params" if test "$start_klogd" != no ; then echo "ExecReload=/bin/systemctl kill --signal=TSTP klogd.service" fi echo "ExecReload=/sbin/killproc -p /var/run/${syslog_bin##*/}.pid -HUP $syslog_bin" if test "$start_klogd" != no ; then echo "ExecReload=/bin/systemctl kill --signal=CONT klogd.service" echo "ExecReload=/bin/systemctl kill --signal=USR2 klogd.service" fi echo echo "[Install]" echo "WantedBy=multi-user.target" exec 1>&3 3>&- if test "$start_klogd" != no -a -x ${klog_bin} ; then trap 'rm -f "$service" "$klogserv"' EXIT klogserv=$(/bin/mktemp $RUN_PATH/klogd.XXXXXXXX) || exit 1 (/bin/cat > $klogserv)<<-EOF # Warning: Do not edit as this service file has been and will be # autogenerated by $0 [Unit] # If klogd.service is started then BindTo=syslog.service # within unit section to make it disappear if syslogd dies. Description=System Kernel Logging Service Requisite=syslog.service BindTo=syslog.service After=syslog.service RefuseManualStart=true [Service] Type=forking StandardOutput=syslog StandardError=syslog PIDFile=/var/run/${klog_bin##*/}.pid Environment=KERNEL_LOGLEVEL=1 Environment=KLOGD_PARAMS= EnvironmentFile=-/etc/sysconfig/syslog ExecStart=${klog_bin} -c \$KERNEL_LOGLEVEL \$KLOGD_PARAMS -x [Install] WantedBy=multi-user.target EOF fi # # Now enable the service(s) for multi-user target # test -d $UNIT_PATH/multi-user.target.wants/ || mkdir -p $UNIT_PATH/multi-user.target.wants /bin/mv $service $RUN_PATH/syslog.service /bin/ln -sf $RUN_PATH/syslog.service $UNIT_PATH/multi-user.target.wants/ if test "$start_klogd" != no ; then /bin/mv $klogserv $RUN_PATH/klogd.service /bin/ln -sf $RUN_PATH/klogd.service $UNIT_PATH/multi-user.target.wants/ fi # # Last but not least remove the old (early) service links from SystemV init # for service in /etc/init.d/*.d/*syslog ; do test -h $service || continue rm -f $service done # # end of /lib/systemd/system-generator/syslog-service-generator