blob: 6950419cecac9001d6d272405563686fb627b245 [file] [log] [blame]
robbiew6f90ff32002-12-09 23:19:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew6f90ff32002-12-09 23:19:12 +000018 */
19
20/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
21/* 11/22/2002 Port to Linux dbarrera@us.ibm.com */
22
Garrett Cooperdf9a3672010-12-19 08:28:53 -080023#include <sys/types.h>
24#include <assert.h>
25#include <fcntl.h>
26#include <signal.h>
robbiew6f90ff32002-12-09 23:19:12 +000027#include <stdio.h>
28#include <syslog.h>
subrata_modaka06f7a02008-01-08 12:29:35 +000029#include <time.h>
Garrett Cooperdf9a3672010-12-19 08:28:53 -080030#include <unistd.h>
robbiew6f90ff32002-12-09 23:19:12 +000031#include "test.h"
robbiew6f90ff32002-12-09 23:19:12 +000032
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020033char *TCID = "syslogtst";
34int TST_TOTAL = 1;
robbiew6f90ff32002-12-09 23:19:12 +000035
subrata_modak4bb656a2009-02-26 12:02:09 +000036void sig_handler(int signal);
robbiew6f90ff32002-12-09 23:19:12 +000037
robbiew9a66de12003-03-27 22:16:14 +000038int main(int argc, char *argv[])
robbiew6f90ff32002-12-09 23:19:12 +000039{
subrata_modak4376ce82008-01-17 08:45:18 +000040 int status, flag3, fd, ch, ch1;
robbiew6f90ff32002-12-09 23:19:12 +000041 int exit_flag = 0; /* used for syslog test case 6. */
subrata_modak4376ce82008-01-17 08:45:18 +000042 time_t t;
robbiew6f90ff32002-12-09 23:19:12 +000043
Garrett Cooperdf9a3672010-12-19 08:28:53 -080044 ch1 = -1;
45
robbiew6f90ff32002-12-09 23:19:12 +000046 signal(SIGINT, sig_handler);
47 signal(SIGTERM, sig_handler);
48 signal(SIGHUP, sig_handler);
49 signal(SIGABRT, sig_handler);
50 signal(SIGSEGV, sig_handler);
51 signal(SIGQUIT, sig_handler);
52
subrata_modak4376ce82008-01-17 08:45:18 +000053 time(&t);
subrata_modak56207ce2009-03-23 13:35:39 +000054 srandom((unsigned int)getpid() ^
55 (((unsigned int)t << 16) | (unsigned int)t >> 16));
robbiew6f90ff32002-12-09 23:19:12 +000056
subrata_modak56207ce2009-03-23 13:35:39 +000057 if (argc < 2) {
subrata_modak4376ce82008-01-17 08:45:18 +000058 ch = (random() % 10) + 1;
subrata_modak56207ce2009-03-23 13:35:39 +000059 if (ch == 2)
60 ch1 = random() % 8;
61 if (ch == 8)
62 ch1 = (random() % 5) + 1;
63 tst_resm(TINFO,
64 "\nrandom numbers were generated for the case numbers : %d, %d\n",
65 ch, ch1);
subrata_modak4376ce82008-01-17 08:45:18 +000066 }
67
subrata_modak56207ce2009-03-23 13:35:39 +000068 else if (argc == 2) {
subrata_modak4376ce82008-01-17 08:45:18 +000069 ch = atoi(argv[1]);
Garrett Cooper732f7db2011-02-24 08:46:43 -080070 if (ch == 2 || ch == 8) {
subrata_modak56207ce2009-03-23 13:35:39 +000071 if (ch == 2)
72 ch1 = random() % 8;
73 if (ch == 8)
74 ch1 = (random() % 5) + 1;
75 tst_resm(TINFO,
76 "\nrandom number was generated for case %d : %d\n",
77 ch, ch1);
subrata_modak391a4462008-03-25 06:42:56 +000078 }
subrata_modak4376ce82008-01-17 08:45:18 +000079 }
subrata_modakbdbaec52009-02-26 12:14:51 +000080
subrata_modak4376ce82008-01-17 08:45:18 +000081 else {
82 ch = atoi(argv[1]);
subrata_modak56207ce2009-03-23 13:35:39 +000083 if (argc > 2)
84 ch1 = atoi(argv[2]);
subrata_modak4376ce82008-01-17 08:45:18 +000085 }
subrata_modak56207ce2009-03-23 13:35:39 +000086
Garrett Cooper732f7db2011-02-24 08:46:43 -080087 /* Ensure ch1 is properly allocated when ch == 2 or ch == 8. */
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 assert(!((ch == 2 || ch == 8) && ch1 == -1));
Garrett Cooperdf9a3672010-12-19 08:28:53 -080089
subrata_modak4376ce82008-01-17 08:45:18 +000090 /*
robbiew6f90ff32002-12-09 23:19:12 +000091 * Send syslog messages according to the case number, which
92 * we will know from command line.
93 */
subrata_modak56207ce2009-03-23 13:35:39 +000094 switch (ch) {
robbiew6f90ff32002-12-09 23:19:12 +000095 case 1:
96 syslog(LOG_MAIL | LOG_INFO, "syslogtst: mail info test.");
97 break;
98 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +000099 switch (ch1) {
robbiew6f90ff32002-12-09 23:19:12 +0000100 case 0:
subrata_modak56207ce2009-03-23 13:35:39 +0000101 syslog(LOG_MAIL | LOG_EMERG,
102 "syslogtst: mail emerg test.");
robbiew6f90ff32002-12-09 23:19:12 +0000103 break;
104 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000105 syslog(LOG_MAIL | LOG_ALERT,
106 "syslogtst: mail alert test.");
robbiew6f90ff32002-12-09 23:19:12 +0000107 break;
108 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000109 syslog(LOG_MAIL | LOG_CRIT,
110 "syslogtst: mail crit test.");
robbiew6f90ff32002-12-09 23:19:12 +0000111 break;
112 case 3:
113 syslog(LOG_MAIL | LOG_ERR, "syslogtst: mail err test.");
114 break;
115 case 4:
subrata_modak56207ce2009-03-23 13:35:39 +0000116 syslog(LOG_MAIL | LOG_WARNING,
117 "syslogtst: mail warning test.");
robbiew6f90ff32002-12-09 23:19:12 +0000118 break;
119 case 5:
subrata_modak56207ce2009-03-23 13:35:39 +0000120 syslog(LOG_MAIL | LOG_NOTICE,
121 "syslogtst: mail notice test.");
robbiew6f90ff32002-12-09 23:19:12 +0000122 break;
123 case 6:
subrata_modak56207ce2009-03-23 13:35:39 +0000124 syslog(LOG_MAIL | LOG_INFO,
125 "syslogtst: mail info test.");
robbiew6f90ff32002-12-09 23:19:12 +0000126 break;
127 case 7:
subrata_modak56207ce2009-03-23 13:35:39 +0000128 syslog(LOG_MAIL | LOG_DEBUG,
129 "syslogtst: mail debug test.");
robbiew6f90ff32002-12-09 23:19:12 +0000130 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000131
robbiew6f90ff32002-12-09 23:19:12 +0000132 }
133 break;
134 case 3:
135 openlog("SYSLOG_CASE3", LOG_PID, LOG_DAEMON);
136 syslog(LOG_DAEMON | LOG_INFO, "syslogtst: daemon info test.");
137 closelog();
138 break;
139 case 4:
140 openlog("log_pid_test", LOG_PID, LOG_USER);
141 syslog(LOG_USER | LOG_INFO, "syslogtst: user info test.");
142 closelog();
143 break;
144 case 5:
145 openlog("log_cons_test", LOG_CONS, LOG_USER);
146
147 /*
148 * Move the /dev/syslog to /dev/syslog.tmp
149 * This way we are forcing syslog to write messages to
150 * console.
151 */
mridge44de5c82005-02-21 17:37:12 +0000152#ifdef DEBUG2
subrata_modak56207ce2009-03-23 13:35:39 +0000153 status =
154 system
155 ("/bin/mv -f /var/log/messages /var/log/messages.tmp");
mridge44de5c82005-02-21 17:37:12 +0000156#else
subrata_modak56207ce2009-03-23 13:35:39 +0000157 status = 0;
mridge44de5c82005-02-21 17:37:12 +0000158#endif
robbiew6f90ff32002-12-09 23:19:12 +0000159 if (status == 0) {
160#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000161 tst_resm(TINFO,
162 "/var/log/messages is moved to /var/log/messages.tmp...");
robbiew6f90ff32002-12-09 23:19:12 +0000163#endif
164 flag3 = 1;
subrata_modak56207ce2009-03-23 13:35:39 +0000165 } else {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100166 tst_brkm(TFAIL,
167 NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000168 "Cannot move /var/log/messages. Setup failed...exiting...");
robbiew6f90ff32002-12-09 23:19:12 +0000169 }
170 sleep(10);
171
172 syslog(LOG_USER | LOG_INFO, "syslogtst: info to console test.");
173
174 sleep(10);
175 /*
176 * Restore /dev/syslog file.
177 */
178 if (flag3 == 1) {
mridge44de5c82005-02-21 17:37:12 +0000179#ifdef DEBUG2
subrata_modak56207ce2009-03-23 13:35:39 +0000180 status =
181 system
182 ("/bin/mv -f /var/log/messages.tmp /var/log/messages");
mridge44de5c82005-02-21 17:37:12 +0000183#else
subrata_modak56207ce2009-03-23 13:35:39 +0000184 status = 0;
mridge44de5c82005-02-21 17:37:12 +0000185#endif
robbiew6f90ff32002-12-09 23:19:12 +0000186 if (status != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100187 tst_brkm(TFAIL,
188 NULL,
189 "Restoring /var/log/messages failed...");
robbiew6f90ff32002-12-09 23:19:12 +0000190 }
191#ifdef DEBUG
192 else
vapiercff4af02006-02-11 04:46:30 +0000193 tst_resm(TINFO, "/var/log/messages restored..");
robbiew6f90ff32002-12-09 23:19:12 +0000194#endif
195 }
196 closelog();
197 break;
198 case 6:
199 openlog("without log_ndelay", LOG_PID, LOG_USER);
subrata_modak023aa322009-01-22 08:57:13 +0000200 fd = open("/dev/null", O_RDONLY);
robbiew6f90ff32002-12-09 23:19:12 +0000201#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000202 tst_resm(TINFO, "openlog() without LOG_NDELAY option...");
robbiew6f90ff32002-12-09 23:19:12 +0000203#endif
robbiew0c12e422002-12-13 16:29:25 +0000204 if (fd >= 3) {
robbiew6f90ff32002-12-09 23:19:12 +0000205#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000206 tst_resm(TINFO,
207 "open() has returned the expected fd: %d", fd);
robbiew6f90ff32002-12-09 23:19:12 +0000208#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000209 } else {
210 tst_resm(TFAIL, "open() has returned unexpected fd: %d",
211 fd);
robbiew6f90ff32002-12-09 23:19:12 +0000212 exit_flag = 1;
213 close(fd);
214 closelog();
215 break;
216 }
217 close(fd);
218 closelog();
219
220 openlog("with log_ndelay", LOG_NDELAY, LOG_USER);
subrata_modak023aa322009-01-22 08:57:13 +0000221 fd = open("/dev/null", O_RDONLY);
robbiew6f90ff32002-12-09 23:19:12 +0000222#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000223 tst_resm(TINFO, "openlog() with LOG_NDELAY option...");
robbiew6f90ff32002-12-09 23:19:12 +0000224#endif
225 if (fd <= 3) {
subrata_modak56207ce2009-03-23 13:35:39 +0000226 tst_resm(TFAIL, "open() returned unexpected fd: %d",
227 fd);
robbiew6f90ff32002-12-09 23:19:12 +0000228 exit_flag = 1;
229 close(fd);
230 closelog();
231 break;
232 }
233#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000234 else
235 tst_resm(TINFO, "open() has returned expected fd: %d",
236 fd);
robbiew6f90ff32002-12-09 23:19:12 +0000237#endif
238 close(fd);
239 closelog();
240 break;
241 case 7:
242 syslog(LOG_USER | LOG_EMERG, "syslogtst: emergency log");
243 syslog(LOG_USER | LOG_ALERT, "syslogtst: alert log");
244 syslog(LOG_USER | LOG_CRIT, "syslogtst: critical log");
245 syslog(LOG_USER | LOG_ERR, "syslogtst: error log");
246 syslog(LOG_USER | LOG_WARNING, "syslogtst: warning log");
247 syslog(LOG_USER | LOG_NOTICE, "syslogtst: notice log");
248 syslog(LOG_USER | LOG_INFO, "syslogtst: info log");
249 syslog(LOG_USER | LOG_DEBUG, "syslogtst: debug log");
250 break;
251 case 8:
subrata_modak56207ce2009-03-23 13:35:39 +0000252 switch (ch1) {
253 /*
254 * Kernel messages cannot be send by user, so skipping the
255 * LOG_KERN facility.
256 */
robbiew6f90ff32002-12-09 23:19:12 +0000257 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000258 syslog(LOG_USER | LOG_INFO,
259 "syslogtst: user info test.");
260 break;
robbiew6f90ff32002-12-09 23:19:12 +0000261 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000262 syslog(LOG_MAIL | LOG_INFO,
263 "syslogtst: mail info test.");
264 break;
robbiew6f90ff32002-12-09 23:19:12 +0000265 case 3:
subrata_modak56207ce2009-03-23 13:35:39 +0000266 syslog(LOG_DAEMON | LOG_INFO,
267 "syslogtst: daemon info test.");
268 break;
robbiew6f90ff32002-12-09 23:19:12 +0000269 case 4:
subrata_modak56207ce2009-03-23 13:35:39 +0000270 syslog(LOG_AUTH | LOG_INFO,
271 "syslogtst: auth info test.");
272 break;
robbiew6f90ff32002-12-09 23:19:12 +0000273 case 5:
274 syslog(LOG_LPR | LOG_INFO, "syslogtst: lpr info test.");
subrata_modak56207ce2009-03-23 13:35:39 +0000275 break;
robbiew6f90ff32002-12-09 23:19:12 +0000276 }
277 break;
278 case 9:
279 setlogmask(LOG_UPTO(LOG_ERR));
280 syslog(LOG_USER | LOG_ERR, "syslogtst: error level is logged");
subrata_modak56207ce2009-03-23 13:35:39 +0000281 syslog(LOG_USER | LOG_WARNING,
282 "syslogtst: warning level not to be logged");
robbiew6f90ff32002-12-09 23:19:12 +0000283 break;
284 case 10:
285 setlogmask(LOG_MASK(LOG_ERR));
subrata_modak56207ce2009-03-23 13:35:39 +0000286 syslog(LOG_USER | LOG_ERR,
287 "syslogtst:10 error level is logged");
288 syslog(LOG_USER | LOG_WARNING,
289 "syslogtst:10 warning level not to be logged");
robbiew6f90ff32002-12-09 23:19:12 +0000290 break;
291 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000292
robbiew6f90ff32002-12-09 23:19:12 +0000293 /*
294 * Check the exit_flag and if it is set,
295 * exit with status 1, indicating failure.
296 */
subrata_modak56207ce2009-03-23 13:35:39 +0000297 if (exit_flag == 1)
298 exit(1);
299 else
300 exit(0);
robbiew6f90ff32002-12-09 23:19:12 +0000301
subrata_modak56207ce2009-03-23 13:35:39 +0000302}
robbiew6f90ff32002-12-09 23:19:12 +0000303
304void sig_handler(int signal)
305{
subrata_modakbdbaec52009-02-26 12:14:51 +0000306
subrata_modak56207ce2009-03-23 13:35:39 +0000307 switch (signal) {
robbiew6f90ff32002-12-09 23:19:12 +0000308 case SIGINT:
309#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000310 tst_resm(TINFO, "SIGINT is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000311#endif
312 break;
313 case SIGTERM:
314#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000315 tst_resm(TINFO, "SIGTERM is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000316#endif
317 break;
318 case SIGHUP:
319#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000320 tst_resm(TINFO, "SIGHUP is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000321#endif
322 break;
323 case SIGABRT:
324#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000325 tst_resm(TINFO, "SIGABRT is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000326#endif
327 break;
328 case SIGSEGV:
329#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000330 tst_resm(TINFO, "SIGSEGV is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000331#endif
332 break;
333 case SIGQUIT:
334#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000335 tst_resm(TINFO, "SIGQUIT is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000336#endif
337 break;
338 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000339
robbiew6f90ff32002-12-09 23:19:12 +0000340 exit(signal);
Garrett Cooper732f7db2011-02-24 08:46:43 -0800341}