• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
21 /* 11/22/2002	Port to Linux	dbarrera@us.ibm.com */
22 
23 #include <sys/types.h>
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <syslog.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include "test.h"
32 
33 char *TCID = "syslogtst";
34 int TST_TOTAL = 1;
35 
36 void sig_handler(int signal);
37 
main(int argc,char * argv[])38 int main(int argc, char *argv[])
39 {
40 	int status, flag3, fd, ch, ch1;
41 	int exit_flag = 0;	/* used for syslog test case 6. */
42 	time_t t;
43 
44 	ch1 = -1;
45 
46 	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 
53 	time(&t);
54 	srandom((unsigned int)getpid() ^
55 		(((unsigned int)t << 16) | (unsigned int)t >> 16));
56 
57 	if (argc < 2) {
58 		ch = (random() % 10) + 1;
59 		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);
66 	}
67 
68 	else if (argc == 2) {
69 		ch = atoi(argv[1]);
70 		if (ch == 2 || ch == 8) {
71 			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);
78 		}
79 	}
80 
81 	else {
82 		ch = atoi(argv[1]);
83 		if (argc > 2)
84 			ch1 = atoi(argv[2]);
85 	}
86 
87 	/* Ensure ch1 is properly allocated when ch == 2 or ch == 8. */
88 	assert(!((ch == 2 || ch == 8) && ch1 == -1));
89 
90 	/*
91 	 * Send syslog messages according to the case number, which
92 	 * we will know from command line.
93 	 */
94 	switch (ch) {
95 	case 1:
96 		syslog(LOG_MAIL | LOG_INFO, "syslogtst: mail info test.");
97 		break;
98 	case 2:
99 		switch (ch1) {
100 		case 0:
101 			syslog(LOG_MAIL | LOG_EMERG,
102 			       "syslogtst: mail emerg test.");
103 			break;
104 		case 1:
105 			syslog(LOG_MAIL | LOG_ALERT,
106 			       "syslogtst: mail alert test.");
107 			break;
108 		case 2:
109 			syslog(LOG_MAIL | LOG_CRIT,
110 			       "syslogtst: mail crit test.");
111 			break;
112 		case 3:
113 			syslog(LOG_MAIL | LOG_ERR, "syslogtst: mail err test.");
114 			break;
115 		case 4:
116 			syslog(LOG_MAIL | LOG_WARNING,
117 			       "syslogtst: mail warning test.");
118 			break;
119 		case 5:
120 			syslog(LOG_MAIL | LOG_NOTICE,
121 			       "syslogtst: mail notice test.");
122 			break;
123 		case 6:
124 			syslog(LOG_MAIL | LOG_INFO,
125 			       "syslogtst: mail info test.");
126 			break;
127 		case 7:
128 			syslog(LOG_MAIL | LOG_DEBUG,
129 			       "syslogtst: mail debug test.");
130 			break;
131 
132 		}
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 		 */
152 #ifdef DEBUG2
153 		status =
154 		    system
155 		    ("/bin/mv -f /var/log/messages /var/log/messages.tmp");
156 #else
157 		status = 0;
158 #endif
159 		if (status == 0) {
160 #ifdef DEBUG
161 			tst_resm(TINFO,
162 				 "/var/log/messages is moved to /var/log/messages.tmp...");
163 #endif
164 			flag3 = 1;
165 		} else {
166 			tst_brkm(TFAIL,
167 				 NULL,
168 				 "Cannot move /var/log/messages. Setup failed...exiting...");
169 		}
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) {
179 #ifdef DEBUG2
180 			status =
181 			    system
182 			    ("/bin/mv -f /var/log/messages.tmp /var/log/messages");
183 #else
184 			status = 0;
185 #endif
186 			if (status != 0) {
187 				tst_brkm(TFAIL,
188 					 NULL,
189 				         "Restoring /var/log/messages failed...");
190 			}
191 #ifdef DEBUG
192 			else
193 				tst_resm(TINFO, "/var/log/messages restored..");
194 #endif
195 		}
196 		closelog();
197 		break;
198 	case 6:
199 		openlog("without log_ndelay", LOG_PID, LOG_USER);
200 		fd = open("/dev/null", O_RDONLY);
201 #ifdef DEBUG
202 		tst_resm(TINFO, "openlog() without LOG_NDELAY option...");
203 #endif
204 		if (fd >= 3) {
205 #ifdef DEBUG
206 			tst_resm(TINFO,
207 				 "open() has returned the expected fd: %d", fd);
208 #endif
209 		} else {
210 			tst_resm(TFAIL, "open() has returned unexpected fd: %d",
211 				 fd);
212 			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);
221 		fd = open("/dev/null", O_RDONLY);
222 #ifdef DEBUG
223 		tst_resm(TINFO, "openlog() with LOG_NDELAY option...");
224 #endif
225 		if (fd <= 3) {
226 			tst_resm(TFAIL, "open() returned unexpected fd: %d",
227 				 fd);
228 			exit_flag = 1;
229 			close(fd);
230 			closelog();
231 			break;
232 		}
233 #ifdef DEBUG
234 		else
235 			tst_resm(TINFO, "open() has returned expected fd: %d",
236 				 fd);
237 #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:
252 		switch (ch1) {
253 			/*
254 			 * Kernel messages cannot be send by user, so skipping the
255 			 * LOG_KERN facility.
256 			 */
257 		case 1:
258 			syslog(LOG_USER | LOG_INFO,
259 			       "syslogtst: user info test.");
260 			break;
261 		case 2:
262 			syslog(LOG_MAIL | LOG_INFO,
263 			       "syslogtst: mail info test.");
264 			break;
265 		case 3:
266 			syslog(LOG_DAEMON | LOG_INFO,
267 			       "syslogtst: daemon info test.");
268 			break;
269 		case 4:
270 			syslog(LOG_AUTH | LOG_INFO,
271 			       "syslogtst: auth info test.");
272 			break;
273 		case 5:
274 			syslog(LOG_LPR | LOG_INFO, "syslogtst: lpr info test.");
275 			break;
276 		}
277 		break;
278 	case 9:
279 		setlogmask(LOG_UPTO(LOG_ERR));
280 		syslog(LOG_USER | LOG_ERR, "syslogtst: error level is logged");
281 		syslog(LOG_USER | LOG_WARNING,
282 		       "syslogtst: warning level not to be logged");
283 		break;
284 	case 10:
285 		setlogmask(LOG_MASK(LOG_ERR));
286 		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");
290 		break;
291 	}
292 
293 	/*
294 	 * Check the exit_flag and if it is set,
295 	 * exit with status 1, indicating failure.
296 	 */
297 	if (exit_flag == 1)
298 		exit(1);
299 	else
300 		exit(0);
301 
302 }
303 
sig_handler(int signal)304 void sig_handler(int signal)
305 {
306 
307 	switch (signal) {
308 	case SIGINT:
309 #ifdef DEBUG
310 		tst_resm(TINFO, "SIGINT is received.");
311 #endif
312 		break;
313 	case SIGTERM:
314 #ifdef DEBUG
315 		tst_resm(TINFO, "SIGTERM is received.");
316 #endif
317 		break;
318 	case SIGHUP:
319 #ifdef DEBUG
320 		tst_resm(TINFO, "SIGHUP is received.");
321 #endif
322 		break;
323 	case SIGABRT:
324 #ifdef DEBUG
325 		tst_resm(TINFO, "SIGABRT is received.");
326 #endif
327 		break;
328 	case SIGSEGV:
329 #ifdef DEBUG
330 		tst_resm(TINFO, "SIGSEGV is received.");
331 #endif
332 		break;
333 	case SIGQUIT:
334 #ifdef DEBUG
335 		tst_resm(TINFO, "SIGQUIT is received.");
336 #endif
337 		break;
338 	}
339 
340 	exit(signal);
341 }
342