Home | History | Annotate | Line # | Download | only in screenblank
screenblank.c revision 1.26
      1 /*	$NetBSD: screenblank.c,v 1.26 2006/09/24 01:57:03 uwe Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996-2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Screensaver daemon for the Sun 3 and SPARC, and platforms using WSCONS.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 #ifndef lint
     45 __COPYRIGHT(
     46 "@(#) Copyright (c) 1996-2002 \
     47 	The NetBSD Foundation, Inc.  All rights reserved.");
     48 __RCSID("$NetBSD: screenblank.c,v 1.26 2006/09/24 01:57:03 uwe Exp $");
     49 #endif
     50 
     51 #include <sys/types.h>
     52 #include <sys/time.h>
     53 #include <sys/stat.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/queue.h>
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <errno.h>
     59 #include <fcntl.h>
     60 #include <limits.h>
     61 #include <math.h>
     62 #include <paths.h>
     63 #include <stdlib.h>
     64 #include <stdio.h>
     65 #include <string.h>
     66 #include <signal.h>
     67 #include <syslog.h>
     68 #include <unistd.h>
     69 #include <util.h>
     70 
     71 #include <dev/wscons/wsconsio.h>
     72 
     73 #ifdef HAVE_FBIO
     74 #include <dev/sun/fbio.h>
     75 #endif
     76 
     77 #include "pathnames.h"
     78 
     79 u_long	setvideo = WSDISPLAYIO_SVIDEO;		/* "set video" ioctl */
     80 int	videoon  = WSDISPLAYIO_VIDEO_ON;	/* value for "on" */
     81 int	videooff = WSDISPLAYIO_VIDEO_OFF;	/* value for "off" */
     82 
     83 struct	dev_stat {
     84 	LIST_ENTRY(dev_stat) ds_link;	/* linked list */
     85 	const char *ds_path;		/* path to device */
     86 	int	ds_isfb;		/* boolean; framebuffer? */
     87 	time_t	ds_atime;		/* time device last accessed */
     88 	time_t	ds_mtime;		/* time device last modified */
     89 };
     90 LIST_HEAD(ds_list, dev_stat) ds_list;
     91 
     92 int	main(int, char *[]);
     93 static	void add_dev(const char *, int);
     94 static	void change_state(int);
     95 static	void cvt_arg(char *, struct timespec *);
     96 static	void sighandler(int);
     97 static	int is_graphics_fb(struct dev_stat *);
     98 static	void usage(void);
     99 
    100 int
    101 main(int argc, char *argv[])
    102 {
    103 	struct dev_stat *dsp;
    104 	struct timespec timo_on, timo_off, *tvp, tv;
    105 	struct sigaction sa;
    106 	struct stat st;
    107 	int ch, change, fflag = 0, kflag = 0, mflag = 0, state;
    108 	int bflag = 0, uflag = 0;
    109 	const char *kbd, *mouse, *display;
    110 
    111 	LIST_INIT(&ds_list);
    112 
    113 	/*
    114 	 * Set the default timeouts: 10 minutes on, .25 seconds off.
    115 	 */
    116 	timo_on.tv_sec = 600;
    117 	timo_on.tv_nsec = 0;
    118 	timo_off.tv_sec = 0;
    119 	timo_off.tv_nsec = 250000000;
    120 
    121 	while ((ch = getopt(argc, argv, "bd:e:f:i:kmu")) != -1) {
    122 		switch (ch) {
    123 		case 'b':
    124 			bflag = 1;
    125 			uflag = 0;
    126 			break;
    127 
    128 		case 'd':
    129 			cvt_arg(optarg, &timo_on);
    130 			break;
    131 
    132 		case 'e':
    133 			cvt_arg(optarg, &timo_off);
    134 			break;
    135 
    136 		case 'f':
    137 			fflag = 1;
    138 			add_dev(optarg, 1);
    139 			break;
    140 
    141 		case 'i':
    142 			add_dev(optarg, 0);
    143 			break;
    144 
    145 		case 'k':
    146 			if (mflag || kflag)
    147 				usage();
    148 			kflag = 1;
    149 			break;
    150 
    151 		case 'm':
    152 			if (kflag || mflag)
    153 				usage();
    154 			mflag = 1;
    155 			break;
    156 
    157 		case 'u':
    158 			uflag = 1;
    159 			bflag = 0;
    160 			break;
    161 
    162 		default:
    163 			usage();
    164 		}
    165 	}
    166 	argc -= optind;
    167 	if (argc)
    168 		usage();
    169 
    170 	/*
    171 	 * Default to WSCONS support.
    172 	 */
    173 	kbd = _PATH_WSKBD;
    174 	mouse = _PATH_WSMOUSE;
    175 	display = _PATH_WSDISPLAY;
    176 
    177 #ifdef HAVE_FBIO
    178 	/*
    179 	 * If a display device wasn't specified, check to see which we
    180 	 * have.  If we can't open the WSCONS display, fall back to fbio.
    181 	 */
    182 	if (!fflag) {
    183 		int fd;
    184 
    185 		if ((fd = open(display, O_RDONLY, 0666)) == -1)
    186 			setvideo = FBIOSVIDEO;
    187 		else
    188 			(void) close(fd);
    189 	}
    190 
    191 	/*
    192 	 * Do this here so that -f ... args above can influence us.
    193 	 */
    194 	if (setvideo == FBIOSVIDEO) {
    195 		videoon = FBVIDEO_ON;
    196 		videooff = FBVIDEO_OFF;
    197 		kbd = _PATH_KEYBOARD;
    198 		mouse = _PATH_MOUSE;
    199 		display = _PATH_FB;
    200 	}
    201 #endif
    202 
    203 	/*
    204 	 * Add the default framebuffer device if necessary.
    205 	 * We _always_ check the console device.
    206 	 */
    207 	add_dev(_PATH_CONSOLE, 0);
    208 	if (!fflag)
    209 		add_dev(display, 1);
    210 
    211 	/*
    212 	 * If this is an one-off blank/unblank request, handle it now.
    213 	 * We don't need to open keyboard/mouse device for that.
    214 	 */
    215 	if (bflag || uflag) {
    216 		change_state(bflag ? videooff : videoon);
    217 		exit(0);
    218 	}
    219 
    220 
    221 	/* Add the keyboard and mouse devices as necessary. */
    222 	if (!kflag)
    223 		add_dev(kbd, 0);
    224 	if (!mflag)
    225 		add_dev(mouse, 0);
    226 
    227 	/* Ensure that the framebuffer is on. */
    228 	state = videoon;
    229 	change_state(state);
    230 	tvp = &timo_on;
    231 
    232 	/*
    233 	 * Make sure the framebuffer gets turned back on when we're
    234 	 * killed.
    235 	 */
    236 	sa.sa_handler = sighandler;
    237 	sa.sa_flags = SA_NOCLDSTOP;
    238 	if (sigemptyset(&sa.sa_mask))
    239 		err(1, "sigemptyset");
    240 	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
    241 	    sigaction(SIGHUP, &sa, NULL))
    242 		err(1, "sigaction");
    243 
    244 	openlog("screenblank", LOG_PID, LOG_DAEMON);
    245 	/* Detach. */
    246 	if (daemon(0, 0))
    247 		err(1, "daemon");
    248 	pidfile(NULL);
    249 
    250 	/* Start the state machine. */
    251 	for (;;) {
    252 		change = 0;
    253 		LIST_FOREACH(dsp, &ds_list, ds_link) {
    254 			/* Don't check framebuffers in graphics mode. */
    255 			if (is_graphics_fb(dsp))
    256 				continue;
    257 			if (stat(dsp->ds_path, &st) == -1) {
    258 				syslog(LOG_CRIT,
    259 				    "Can't stat `%s' (%m)", dsp->ds_path);
    260 				exit(1);
    261 			}
    262 			if (st.st_atime > dsp->ds_atime) {
    263 				change = 1;
    264 				dsp->ds_atime = st.st_atime;
    265 			}
    266 			if (st.st_mtime > dsp->ds_mtime) {
    267 				change = 1;
    268 				dsp->ds_mtime = st.st_mtime;
    269 			}
    270 		}
    271 
    272 		if (state == videoon) {
    273 			if (!change) {
    274 				state = videooff;
    275 				change_state(state);
    276 				tvp = &timo_off;
    277 			}
    278 		} else {
    279 			if (change) {
    280 				state = videoon;
    281 				change_state(state);
    282 				tvp = &timo_on;
    283 			}
    284 		}
    285 
    286 		tv = *tvp;
    287 		if (nanosleep(&tv, NULL) == -1)
    288 			err(1, "nanosleep");
    289 	}
    290 	/* NOTREACHED */
    291 }
    292 
    293 static void
    294 add_dev(const char *path, int isfb)
    295 {
    296 	struct dev_stat *dsp;
    297 	struct stat sb;
    298 
    299 	/* Make sure we can stat the device. */
    300 	if (stat(path, &sb) == -1) {
    301 		warn("Can't stat `%s'", path);
    302 		return;
    303 	}
    304 
    305 #ifdef HAVE_FBIO
    306 	/*
    307 	 * We default to WSCONS.  If this is a frame buffer
    308 	 * device, check to see if it responds to the old
    309 	 * Sun-style fbio ioctls.  If so, switch to fbio mode.
    310 	 */
    311 	if (isfb && setvideo != FBIOSVIDEO) {
    312 		int onoff, fd;
    313 
    314 		if ((fd = open(path, O_RDWR, 0666)) == -1) {
    315 			warn("Can't open `%s'", path);
    316 			return;
    317 		}
    318 		if ((ioctl(fd, FBIOGVIDEO, &onoff)) == 0)
    319 			setvideo = FBIOSVIDEO;
    320 		(void)close(fd);
    321 	}
    322 #endif
    323 
    324 	/* Create the entry... */
    325 	dsp = malloc(sizeof(struct dev_stat));
    326 	if (dsp == NULL)
    327 		err(1, "Can't allocate memory for `%s'", path);
    328 	(void)memset(dsp, 0, sizeof(struct dev_stat));
    329 	dsp->ds_path = path;
    330 	dsp->ds_isfb = isfb;
    331 
    332 	/* ...and put it in the list. */
    333 	LIST_INSERT_HEAD(&ds_list, dsp, ds_link);
    334 }
    335 
    336 /* ARGSUSED */
    337 static void
    338 sighandler(int sig)
    339 {
    340 
    341 	/* Kill the pid file and re-enable the framebuffer before exit. */
    342 	change_state(videoon);
    343 	exit(0);
    344 }
    345 
    346 /*
    347  * Return 1 if we are a framebuffer in graphics mode or a framebuffer
    348  * where we cannot tell the mode. Return 0 if we are not a framebuffer
    349  * device, or a wscons framebuffer in text mode.
    350  */
    351 static int
    352 is_graphics_fb(struct dev_stat *dsp)
    353 {
    354 	int fd;
    355 	int state;
    356 
    357 	if (dsp->ds_isfb == 0)
    358 		return 0;
    359 
    360 	/* We can't tell if we are not a wscons device */
    361 	if (setvideo != WSDISPLAYIO_SVIDEO)
    362 		return 1;
    363 
    364 	if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
    365 		syslog(LOG_WARNING, "Cannot open `%s' (%m)", dsp->ds_path);
    366 		return 1;
    367 	}
    368 
    369 	if (ioctl(fd, WSDISPLAYIO_GMODE, &state) == -1) {
    370 		syslog(LOG_WARNING, "Cannot get mode on `%s' (%m)",
    371 		    dsp->ds_path);
    372 		/* We can't tell, so we say we are mapped */
    373 		state = WSDISPLAYIO_MODE_MAPPED;
    374 	}
    375 
    376 	(void)close(fd);
    377 
    378 	return state != WSDISPLAYIO_MODE_EMUL;
    379 }
    380 
    381 static void
    382 change_state(int state)
    383 {
    384 	struct dev_stat *dsp;
    385 	int fd;
    386 	int fail = 1;
    387 
    388 	LIST_FOREACH(dsp, &ds_list, ds_link) {
    389 		/* Don't change the state of non-framebuffers! */
    390 		if (dsp->ds_isfb == 0)
    391 			continue;
    392 		if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
    393 			syslog(LOG_WARNING, "Can't open `%s' (%m)",
    394 			    dsp->ds_path);
    395 			continue;
    396 		}
    397 		if (ioctl(fd, setvideo, &state) == -1)
    398 			syslog(LOG_WARNING, "Can't set video on `%s' (%m)",
    399 			    dsp->ds_path);
    400 		else
    401 			fail = 0;
    402 		(void)close(fd);
    403 	}
    404 	if (fail) {
    405 		syslog(LOG_CRIT, "No frame buffer devices, exiting\n");
    406 		exit(1);
    407 	}
    408 }
    409 
    410 static void
    411 cvt_arg(char *arg, struct timespec *tvp)
    412 {
    413 	char *cp;
    414 	int seconds, nanoseconds, factor;
    415 	int period = 0;
    416 	factor = 1000000000;
    417 	nanoseconds = 0;
    418 	seconds = 0;
    419 
    420 	for (cp = arg; *cp != '\0'; ++cp) {
    421 		if (*cp == '.') {
    422 			if (period)
    423 				errx(1, "Invalid argument: %s", arg);
    424 			period = 1;
    425 			continue;
    426 		}
    427 
    428 		if (!isdigit((unsigned char)*cp))
    429 			errx(1, "Invalid argument: %s", arg);
    430 
    431 		if (period) {
    432 			if (factor > 1) {
    433 				nanoseconds = nanoseconds * 10 + (*cp - '0');
    434 				factor /= 10;
    435 			}
    436 		} else
    437 			seconds = (seconds * 10) + (*cp - '0');
    438 	}
    439 
    440 	tvp->tv_sec = seconds;
    441 	if (factor > 1)
    442 		nanoseconds *= factor;
    443 
    444 	tvp->tv_nsec = nanoseconds;
    445 }
    446 
    447 static void
    448 usage(void)
    449 {
    450 
    451 	(void)fprintf(stderr,
    452 	    "usage: %s [-k | -m] [-d inactivity-timeout] [-e wakeup-delay]\n"
    453 	    "\t\t[-f framebuffer] [-i input-device]\n"
    454 	    "       %s {-b | -u}\n",
    455 	    getprogname(),
    456 	    getprogname());
    457 	exit(1);
    458 }
    459