xref: /openbsd/regress/usr.sbin/syslogd/logflush.c (revision b7041c07)
1 /*	$OpenBSD: logflush.c,v 1.2 2021/10/24 21:24:21 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2021 Alexander Bluhm <bluhm@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 
22 #include <err.h>
23 #include <fcntl.h>
24 #include <paths.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <syslog.h>
29 #include <unistd.h>
30 
31 __dead void
usage()32 usage()
33 {
34 	fprintf(stderr, "usage: %s\n", getprogname());
35 	exit(2);
36 }
37 
38 int
main(int argc,char * argv[])39 main(int argc, char *argv[])
40 {
41 	int pair[2], klog, val;
42 
43 	if (argc != 1)
44 		usage();
45 
46 	if (socketpair(AF_UNIX, SOCK_DGRAM, PF_UNSPEC, pair) == -1)
47 		err(1, "socketpair");
48 
49 	val = 1<<20;
50 	if (setsockopt(pair[0], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) == -1)
51 		err(1, "setsockopt SO_RCVBUF");
52 	if (setsockopt(pair[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) == -1)
53 		err(1, "setsockopt SO_SNDBUF");
54 
55 	if ((klog = open(_PATH_KLOG, O_RDONLY)) == -1)
56 		err(1, "open %s", _PATH_KLOG);
57 	/* Use /dev/klog to register sendsyslog(2) receiver. */
58 	if (ioctl(klog, LIOCSFD, &pair[1]) == -1)
59 		err(1, "ioctl klog LIOCSFD sendsyslog");
60 	close(pair[1]);
61 
62 	/* Send message via libc, flushes log stash in kernel. */
63 	openlog("syslogd-regress", LOG_PID, LOG_SYSLOG);
64 	syslog(LOG_DEBUG, "logflush");
65 
66 	return 0;
67 }
68