blob: fad6fc0c8c45bc66da31f4c0fd8467d8dc354d47 [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"
32#include "usctest.h"
33/*
34 *These globals must be defined in the test.
35 */
36
subrata_modak56207ce2009-03-23 13:35:39 +000037char *TCID = "syslogtst"; /* Test program identifier. */
38int TST_TOTAL = 1; /* Total number of test cases. */
robbiew6f90ff32002-12-09 23:19:12 +000039
subrata_modak56207ce2009-03-23 13:35:39 +000040int exp_enos[] = { 0 }; /* List must end with 0 */
robbiew6f90ff32002-12-09 23:19:12 +000041
subrata_modak4bb656a2009-02-26 12:02:09 +000042void sig_handler(int signal);
robbiew6f90ff32002-12-09 23:19:12 +000043
robbiew9a66de12003-03-27 22:16:14 +000044int main(int argc, char *argv[])
robbiew6f90ff32002-12-09 23:19:12 +000045{
subrata_modak4376ce82008-01-17 08:45:18 +000046 int status, flag3, fd, ch, ch1;
robbiew6f90ff32002-12-09 23:19:12 +000047 int exit_flag = 0; /* used for syslog test case 6. */
subrata_modak4376ce82008-01-17 08:45:18 +000048 time_t t;
robbiew6f90ff32002-12-09 23:19:12 +000049
Garrett Cooperdf9a3672010-12-19 08:28:53 -080050 ch1 = -1;
51
robbiew6f90ff32002-12-09 23:19:12 +000052 signal(SIGINT, sig_handler);
53 signal(SIGTERM, sig_handler);
54 signal(SIGHUP, sig_handler);
55 signal(SIGABRT, sig_handler);
56 signal(SIGSEGV, sig_handler);
57 signal(SIGQUIT, sig_handler);
58
subrata_modak4376ce82008-01-17 08:45:18 +000059 time(&t);
subrata_modak56207ce2009-03-23 13:35:39 +000060 srandom((unsigned int)getpid() ^
61 (((unsigned int)t << 16) | (unsigned int)t >> 16));
robbiew6f90ff32002-12-09 23:19:12 +000062
subrata_modak56207ce2009-03-23 13:35:39 +000063 if (argc < 2) {
subrata_modak4376ce82008-01-17 08:45:18 +000064 ch = (random() % 10) + 1;
subrata_modak56207ce2009-03-23 13:35:39 +000065 if (ch == 2)
66 ch1 = random() % 8;
67 if (ch == 8)
68 ch1 = (random() % 5) + 1;
69 tst_resm(TINFO,
70 "\nrandom numbers were generated for the case numbers : %d, %d\n",
71 ch, ch1);
subrata_modak4376ce82008-01-17 08:45:18 +000072 }
73
subrata_modak56207ce2009-03-23 13:35:39 +000074 else if (argc == 2) {
subrata_modak4376ce82008-01-17 08:45:18 +000075 ch = atoi(argv[1]);
Garrett Cooper732f7db2011-02-24 08:46:43 -080076 if (ch == 2 || ch == 8) {
subrata_modak56207ce2009-03-23 13:35:39 +000077 if (ch == 2)
78 ch1 = random() % 8;
79 if (ch == 8)
80 ch1 = (random() % 5) + 1;
81 tst_resm(TINFO,
82 "\nrandom number was generated for case %d : %d\n",
83 ch, ch1);
subrata_modak391a4462008-03-25 06:42:56 +000084 }
subrata_modak4376ce82008-01-17 08:45:18 +000085 }
subrata_modakbdbaec52009-02-26 12:14:51 +000086
subrata_modak4376ce82008-01-17 08:45:18 +000087 else {
88 ch = atoi(argv[1]);
subrata_modak56207ce2009-03-23 13:35:39 +000089 if (argc > 2)
90 ch1 = atoi(argv[2]);
subrata_modak4376ce82008-01-17 08:45:18 +000091 }
subrata_modak56207ce2009-03-23 13:35:39 +000092
Garrett Cooper732f7db2011-02-24 08:46:43 -080093 /* Ensure ch1 is properly allocated when ch == 2 or ch == 8. */
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 assert(!((ch == 2 || ch == 8) && ch1 == -1));
Garrett Cooperdf9a3672010-12-19 08:28:53 -080095
subrata_modak4376ce82008-01-17 08:45:18 +000096 /*
robbiew6f90ff32002-12-09 23:19:12 +000097 * Send syslog messages according to the case number, which
98 * we will know from command line.
99 */
subrata_modak56207ce2009-03-23 13:35:39 +0000100 switch (ch) {
robbiew6f90ff32002-12-09 23:19:12 +0000101 case 1:
102 syslog(LOG_MAIL | LOG_INFO, "syslogtst: mail info test.");
103 break;
104 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000105 switch (ch1) {
robbiew6f90ff32002-12-09 23:19:12 +0000106 case 0:
subrata_modak56207ce2009-03-23 13:35:39 +0000107 syslog(LOG_MAIL | LOG_EMERG,
108 "syslogtst: mail emerg test.");
robbiew6f90ff32002-12-09 23:19:12 +0000109 break;
110 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000111 syslog(LOG_MAIL | LOG_ALERT,
112 "syslogtst: mail alert test.");
robbiew6f90ff32002-12-09 23:19:12 +0000113 break;
114 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000115 syslog(LOG_MAIL | LOG_CRIT,
116 "syslogtst: mail crit test.");
robbiew6f90ff32002-12-09 23:19:12 +0000117 break;
118 case 3:
119 syslog(LOG_MAIL | LOG_ERR, "syslogtst: mail err test.");
120 break;
121 case 4:
subrata_modak56207ce2009-03-23 13:35:39 +0000122 syslog(LOG_MAIL | LOG_WARNING,
123 "syslogtst: mail warning test.");
robbiew6f90ff32002-12-09 23:19:12 +0000124 break;
125 case 5:
subrata_modak56207ce2009-03-23 13:35:39 +0000126 syslog(LOG_MAIL | LOG_NOTICE,
127 "syslogtst: mail notice test.");
robbiew6f90ff32002-12-09 23:19:12 +0000128 break;
129 case 6:
subrata_modak56207ce2009-03-23 13:35:39 +0000130 syslog(LOG_MAIL | LOG_INFO,
131 "syslogtst: mail info test.");
robbiew6f90ff32002-12-09 23:19:12 +0000132 break;
133 case 7:
subrata_modak56207ce2009-03-23 13:35:39 +0000134 syslog(LOG_MAIL | LOG_DEBUG,
135 "syslogtst: mail debug test.");
robbiew6f90ff32002-12-09 23:19:12 +0000136 break;
subrata_modak56207ce2009-03-23 13:35:39 +0000137
robbiew6f90ff32002-12-09 23:19:12 +0000138 }
139 break;
140 case 3:
141 openlog("SYSLOG_CASE3", LOG_PID, LOG_DAEMON);
142 syslog(LOG_DAEMON | LOG_INFO, "syslogtst: daemon info test.");
143 closelog();
144 break;
145 case 4:
146 openlog("log_pid_test", LOG_PID, LOG_USER);
147 syslog(LOG_USER | LOG_INFO, "syslogtst: user info test.");
148 closelog();
149 break;
150 case 5:
151 openlog("log_cons_test", LOG_CONS, LOG_USER);
152
153 /*
154 * Move the /dev/syslog to /dev/syslog.tmp
155 * This way we are forcing syslog to write messages to
156 * console.
157 */
mridge44de5c82005-02-21 17:37:12 +0000158#ifdef DEBUG2
subrata_modak56207ce2009-03-23 13:35:39 +0000159 status =
160 system
161 ("/bin/mv -f /var/log/messages /var/log/messages.tmp");
mridge44de5c82005-02-21 17:37:12 +0000162#else
subrata_modak56207ce2009-03-23 13:35:39 +0000163 status = 0;
mridge44de5c82005-02-21 17:37:12 +0000164#endif
robbiew6f90ff32002-12-09 23:19:12 +0000165 if (status == 0) {
166#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000167 tst_resm(TINFO,
168 "/var/log/messages is moved to /var/log/messages.tmp...");
robbiew6f90ff32002-12-09 23:19:12 +0000169#endif
170 flag3 = 1;
subrata_modak56207ce2009-03-23 13:35:39 +0000171 } else {
172 tst_resm(TFAIL,
173 "Cannot move /var/log/messages. Setup failed...exiting...");
robbiew6f90ff32002-12-09 23:19:12 +0000174 tst_exit();
175 }
176 sleep(10);
177
178 syslog(LOG_USER | LOG_INFO, "syslogtst: info to console test.");
179
180 sleep(10);
181 /*
182 * Restore /dev/syslog file.
183 */
184 if (flag3 == 1) {
mridge44de5c82005-02-21 17:37:12 +0000185#ifdef DEBUG2
subrata_modak56207ce2009-03-23 13:35:39 +0000186 status =
187 system
188 ("/bin/mv -f /var/log/messages.tmp /var/log/messages");
mridge44de5c82005-02-21 17:37:12 +0000189#else
subrata_modak56207ce2009-03-23 13:35:39 +0000190 status = 0;
mridge44de5c82005-02-21 17:37:12 +0000191#endif
robbiew6f90ff32002-12-09 23:19:12 +0000192 if (status != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000193 tst_resm(TFAIL,
194 "Restoring /var/log/messages failed...");
robbiew6f90ff32002-12-09 23:19:12 +0000195 tst_exit();
196 }
197#ifdef DEBUG
198 else
vapiercff4af02006-02-11 04:46:30 +0000199 tst_resm(TINFO, "/var/log/messages restored..");
robbiew6f90ff32002-12-09 23:19:12 +0000200#endif
201 }
202 closelog();
203 break;
204 case 6:
205 openlog("without log_ndelay", LOG_PID, LOG_USER);
subrata_modak023aa322009-01-22 08:57:13 +0000206 fd = open("/dev/null", O_RDONLY);
robbiew6f90ff32002-12-09 23:19:12 +0000207#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000208 tst_resm(TINFO, "openlog() without LOG_NDELAY option...");
robbiew6f90ff32002-12-09 23:19:12 +0000209#endif
robbiew0c12e422002-12-13 16:29:25 +0000210 if (fd >= 3) {
robbiew6f90ff32002-12-09 23:19:12 +0000211#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000212 tst_resm(TINFO,
213 "open() has returned the expected fd: %d", fd);
robbiew6f90ff32002-12-09 23:19:12 +0000214#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000215 } else {
216 tst_resm(TFAIL, "open() has returned unexpected fd: %d",
217 fd);
robbiew6f90ff32002-12-09 23:19:12 +0000218 exit_flag = 1;
219 close(fd);
220 closelog();
221 break;
222 }
223 close(fd);
224 closelog();
225
226 openlog("with log_ndelay", LOG_NDELAY, LOG_USER);
subrata_modak023aa322009-01-22 08:57:13 +0000227 fd = open("/dev/null", O_RDONLY);
robbiew6f90ff32002-12-09 23:19:12 +0000228#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000229 tst_resm(TINFO, "openlog() with LOG_NDELAY option...");
robbiew6f90ff32002-12-09 23:19:12 +0000230#endif
231 if (fd <= 3) {
subrata_modak56207ce2009-03-23 13:35:39 +0000232 tst_resm(TFAIL, "open() returned unexpected fd: %d",
233 fd);
robbiew6f90ff32002-12-09 23:19:12 +0000234 exit_flag = 1;
235 close(fd);
236 closelog();
237 break;
238 }
239#ifdef DEBUG
subrata_modak56207ce2009-03-23 13:35:39 +0000240 else
241 tst_resm(TINFO, "open() has returned expected fd: %d",
242 fd);
robbiew6f90ff32002-12-09 23:19:12 +0000243#endif
244 close(fd);
245 closelog();
246 break;
247 case 7:
248 syslog(LOG_USER | LOG_EMERG, "syslogtst: emergency log");
249 syslog(LOG_USER | LOG_ALERT, "syslogtst: alert log");
250 syslog(LOG_USER | LOG_CRIT, "syslogtst: critical log");
251 syslog(LOG_USER | LOG_ERR, "syslogtst: error log");
252 syslog(LOG_USER | LOG_WARNING, "syslogtst: warning log");
253 syslog(LOG_USER | LOG_NOTICE, "syslogtst: notice log");
254 syslog(LOG_USER | LOG_INFO, "syslogtst: info log");
255 syslog(LOG_USER | LOG_DEBUG, "syslogtst: debug log");
256 break;
257 case 8:
subrata_modak56207ce2009-03-23 13:35:39 +0000258 switch (ch1) {
259 /*
260 * Kernel messages cannot be send by user, so skipping the
261 * LOG_KERN facility.
262 */
robbiew6f90ff32002-12-09 23:19:12 +0000263 case 1:
subrata_modak56207ce2009-03-23 13:35:39 +0000264 syslog(LOG_USER | LOG_INFO,
265 "syslogtst: user info test.");
266 break;
robbiew6f90ff32002-12-09 23:19:12 +0000267 case 2:
subrata_modak56207ce2009-03-23 13:35:39 +0000268 syslog(LOG_MAIL | LOG_INFO,
269 "syslogtst: mail info test.");
270 break;
robbiew6f90ff32002-12-09 23:19:12 +0000271 case 3:
subrata_modak56207ce2009-03-23 13:35:39 +0000272 syslog(LOG_DAEMON | LOG_INFO,
273 "syslogtst: daemon info test.");
274 break;
robbiew6f90ff32002-12-09 23:19:12 +0000275 case 4:
subrata_modak56207ce2009-03-23 13:35:39 +0000276 syslog(LOG_AUTH | LOG_INFO,
277 "syslogtst: auth info test.");
278 break;
robbiew6f90ff32002-12-09 23:19:12 +0000279 case 5:
280 syslog(LOG_LPR | LOG_INFO, "syslogtst: lpr info test.");
subrata_modak56207ce2009-03-23 13:35:39 +0000281 break;
robbiew6f90ff32002-12-09 23:19:12 +0000282 }
283 break;
284 case 9:
285 setlogmask(LOG_UPTO(LOG_ERR));
286 syslog(LOG_USER | LOG_ERR, "syslogtst: error level is logged");
subrata_modak56207ce2009-03-23 13:35:39 +0000287 syslog(LOG_USER | LOG_WARNING,
288 "syslogtst: warning level not to be logged");
robbiew6f90ff32002-12-09 23:19:12 +0000289 break;
290 case 10:
291 setlogmask(LOG_MASK(LOG_ERR));
subrata_modak56207ce2009-03-23 13:35:39 +0000292 syslog(LOG_USER | LOG_ERR,
293 "syslogtst:10 error level is logged");
294 syslog(LOG_USER | LOG_WARNING,
295 "syslogtst:10 warning level not to be logged");
robbiew6f90ff32002-12-09 23:19:12 +0000296 break;
297 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000298
robbiew6f90ff32002-12-09 23:19:12 +0000299 /*
300 * Check the exit_flag and if it is set,
301 * exit with status 1, indicating failure.
302 */
subrata_modak56207ce2009-03-23 13:35:39 +0000303 if (exit_flag == 1)
304 exit(1);
305 else
306 exit(0);
robbiew6f90ff32002-12-09 23:19:12 +0000307
subrata_modak56207ce2009-03-23 13:35:39 +0000308}
robbiew6f90ff32002-12-09 23:19:12 +0000309
310void sig_handler(int signal)
311{
subrata_modakbdbaec52009-02-26 12:14:51 +0000312
subrata_modak56207ce2009-03-23 13:35:39 +0000313 switch (signal) {
robbiew6f90ff32002-12-09 23:19:12 +0000314 case SIGINT:
315#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000316 tst_resm(TINFO, "SIGINT is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000317#endif
318 break;
319 case SIGTERM:
320#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000321 tst_resm(TINFO, "SIGTERM is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000322#endif
323 break;
324 case SIGHUP:
325#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000326 tst_resm(TINFO, "SIGHUP is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000327#endif
328 break;
329 case SIGABRT:
330#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000331 tst_resm(TINFO, "SIGABRT is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000332#endif
333 break;
334 case SIGSEGV:
335#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000336 tst_resm(TINFO, "SIGSEGV is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000337#endif
338 break;
339 case SIGQUIT:
340#ifdef DEBUG
vapiercff4af02006-02-11 04:46:30 +0000341 tst_resm(TINFO, "SIGQUIT is received.");
robbiew6f90ff32002-12-09 23:19:12 +0000342#endif
343 break;
344 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000345
robbiew6f90ff32002-12-09 23:19:12 +0000346 exit(signal);
Garrett Cooper732f7db2011-02-24 08:46:43 -0800347}