syslog-lib.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #! /bin/sh
  2. #
  3. # Copyright (c) Linux Test Project, 2010
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. # the GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ##################################################################
  19. readonly MAILLOG=/var/log/maillog
  20. # Signals to trap.
  21. readonly TRAP_SIGS="1 2 3 6 11 15"
  22. # configuration file for syslog or syslog-ng
  23. CONFIG_FILE=""
  24. # rsyslogd .conf specific args.
  25. RSYSLOG_CONFIG=
  26. # number of seconds to wait for another syslog test to complete
  27. WAIT_COUNT=60
  28. cleanup()
  29. {
  30. # Reentrant cleanup -> bad. Especially since rsyslogd on Fedora 13
  31. # seems to get stuck FOREVER when not running as root. Lame...
  32. disable_traps
  33. exit_code=$1
  34. # Restore the previous syslog daemon state.
  35. if [ -f "$CONFIG_FILE.ltpback" ]; then
  36. if mv "$CONFIG_FILE.ltpback" "$CONFIG_FILE"; then
  37. # Make sure that restart_syslog_daemon doesn't loop
  38. # back to cleanup again.
  39. restart_syslog_daemon "return 1"
  40. # Maintain any nonzero exit codes
  41. if [ $exit_code -ne $? ]; then
  42. exit_code=1
  43. fi
  44. else
  45. exit_code=1
  46. fi
  47. fi
  48. exit $exit_code
  49. }
  50. setup()
  51. {
  52. tst_require_root
  53. trap ' disable_traps
  54. tst_resm TBROK "Testing is terminating due to a signal"
  55. cleanup 1' $TRAP_SIGS || exit 1
  56. if [ "$SYSLOG_DAEMON" = "syslog" ]; then
  57. CONFIG_FILE="/etc/syslog.conf"
  58. elif [ "$SYSLOG_DAEMON" = "syslog-ng" ]; then
  59. CONFIG_FILE="/etc/syslog-ng/syslog-ng.conf"
  60. elif [ "$SYSLOG_DAEMON" = "rsyslog" ]; then
  61. CONFIG_FILE="/etc/rsyslog.conf"
  62. # To cope with systemd-journal, we are looking for either:
  63. # $ModLoad imjournal
  64. # module(load="imjournal"...)
  65. # in rsyslog config, and using those settings.
  66. if grep -qri '^[^#]*modload.*imjournal' /etc/rsyslog.conf /etc/rsyslog.d/; then
  67. RSYSLOG_CONFIG=$(grep -Ehoi "^[^#].*(imjournal|workdirectory).*" -r /etc/rsyslog.conf /etc/rsyslog.d/;
  68. echo '$imjournalRatelimitInterval 0'; \
  69. echo '$ImjournalIgnorePreviousMessages on';)
  70. elif grep -qri '^[^#]*module.*load="imjournal"' /etc/rsyslog.conf /etc/rsyslog.d/; then
  71. RSYSLOG_CONFIG=$(grep -Ehoi "^[^#].*workdirectory.*" -r /etc/rsyslog.conf /etc/rsyslog.d/; \
  72. echo 'module(load="imjournal"'; \
  73. echo ' StateFile="imjournal.state"'; \
  74. echo ' Ratelimit.Interval="0"'; \
  75. echo ' IgnorePreviousMessages="on")')
  76. else
  77. RSYSLOG_CONFIG=$(echo '$ModLoad imuxsock.so'; \
  78. grep -ho "^\$SystemLogSocketName .*" -r /etc/rsyslog.conf /etc/rsyslog.d/ | head -1)
  79. fi
  80. else
  81. tst_resm TCONF "Couldn't find syslogd, syslog-ng or rsyslogd"
  82. cleanup 32
  83. fi
  84. # Back up configuration file
  85. if [ -f "$CONFIG_FILE" ]; then
  86. # Pause if another LTP syslog test is running
  87. while [ -f "$CONFIG_FILE.ltpback" -a $WAIT_COUNT -gt 0 ]; do
  88. : $(( WAIT_COUNT -= 1 ))
  89. sleep 1
  90. done
  91. # Oops -- $CONFIG_FILE.ltpback is still there!
  92. if [ $WAIT_COUNT -eq 0 ]; then
  93. tst_resm TBROK "another syslog test is stuck"
  94. cleanup 1
  95. elif ! cp "$CONFIG_FILE" "$CONFIG_FILE.ltpback"; then
  96. tst_resm TBROK "failed to backup $CONFIG_FILE"
  97. cleanup 1
  98. fi
  99. else
  100. tst_resm TBROK "$CONFIG_FILE not found!"
  101. fi
  102. }
  103. disable_traps()
  104. {
  105. trap - $TRAP_SIGS
  106. }
  107. # For most cases this isn't exotic. If you're running upstart however, you
  108. # might have fun here :).
  109. restart_syslog_daemon()
  110. {
  111. # Default to running `cleanup 1' when dealing with error cases.
  112. if [ $# -eq 0 ]; then
  113. cleanup_command="cleanup 1"
  114. else
  115. cleanup_command=$1
  116. fi
  117. tst_resm TINFO "restarting syslog daemon"
  118. if [ -n "$SYSLOG_DAEMON" ]; then
  119. restart_daemon $SYSLOG_DAEMON
  120. if [ $? -eq 0 ]; then
  121. # XXX: this really shouldn't exist; if *syslogd isn't
  122. # ready once the restart directive has been issued,
  123. # then it needs to be fixed.
  124. sleep 2
  125. else
  126. #
  127. # Some distributions name the service syslog even if
  128. # the package is syslog-ng or rsyslog, so try it once
  129. # more with just syslog.
  130. #
  131. restart_daemon "syslog"
  132. if [ $? -ne 0 ]; then
  133. $cleanup_command
  134. fi
  135. fi
  136. fi
  137. }
  138. export TST_TOTAL=${TST_TOTAL:=1}
  139. export TST_COUNT=1
  140. export TCID=${TCID:="$(basename "$0")"}
  141. . cmdlib.sh