Home | History | Annotate | Line # | Download | only in screenblank
screenblank.c revision 1.24
      1 /*	$NetBSD: screenblank.c,v 1.24 2006/09/23 20:12:15 elad 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.24 2006/09/23 20:12:15 elad 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 	 * Handle -b and -u modes.
    205 	 */
    206 	if (bflag || uflag) {
    207 		change_state(bflag ? videooff : videoon);
    208 		exit(0);
    209 	}
    210 
    211 	/*
    212 	 * Add the keyboard, mouse, and default framebuffer devices
    213 	 * as necessary.  We _always_ check the console device.
    214 	 */
    215 	add_dev(_PATH_CONSOLE, 0);
    216 	if (!kflag)
    217 		add_dev(kbd, 0);
    218 	if (!mflag)
    219 		add_dev(mouse, 0);
    220 	if (!fflag)
    221 		add_dev(display, 1);
    222 
    223 	/* Ensure that the framebuffer is on. */
    224 	state = videoon;
    225 	change_state(state);
    226 	tvp = &timo_on;
    227 
    228 	/*
    229 	 * Make sure the framebuffer gets turned back on when we're
    230 	 * killed.
    231 	 */
    232 	sa.sa_handler = sighandler;
    233 	sa.sa_flags = SA_NOCLDSTOP;
    234 	if (sigemptyset(&sa.sa_mask))
    235 		err(1, "sigemptyset");
    236 	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
    237 	    sigaction(SIGHUP, &sa, NULL))
    238 		err(1, "sigaction");
    239 
    240 	openlog("screenblank", LOG_PID, LOG_DAEMON);
    241 	/* Detach. */
    242 	if (daemon(0, 0))
    243 		err(1, "daemon");
    244 	pidfile(NULL);
    245 
    246 	/* Start the state machine. */
    247 	for (;;) {
    248 		change = 0;
    249 		LIST_FOREACH(dsp, &ds_list, ds_link) {
    250 			/* Don't check framebuffers in graphics mode. */
    251 			if (is_graphics_fb(dsp))
    252 				continue;
    253 			if (stat(dsp->ds_path, &st) == -1) {
    254 				syslog(LOG_CRIT,
    255 				    "Can't stat `%s' (%m)", dsp->ds_path);
    256 				exit(1);
    257 			}
    258 			if (st.st_atime > dsp->ds_atime) {
    259 				change = 1;
    260 				dsp->ds_atime = st.st_atime;
    261 			}
    262 			if (st.st_mtime > dsp->ds_mtime) {
    263 				change = 1;
    264 				dsp->ds_mtime = st.st_mtime;
    265 			}
    266 		}
    267 
    268 		if (state == videoon) {
    269 			if (!change) {
    270 				state = videooff;
    271 				change_state(state);
    272 				tvp = &timo_off;
    273 			}
    274 		} else {
    275 			if (change) {
    276 				state = videoon;
    277 				change_state(state);
    278 				tvp = &timo_on;
    279 			}
    280 		}
    281 
    282 		tv = *tvp;
    283 		if (nanosleep(&tv, NULL) == -1)
    284 			err(1, "nanosleep");
    285 	}
    286 	/* NOTREACHED */
    287 }
    288 
    289 static void
    290 add_dev(const char *path, int isfb)
    291 {
    292 	struct dev_stat *dsp;
    293 	struct stat sb;
    294 
    295 	/* Make sure we can stat the device. */
    296 	if (stat(path, &sb) == -1) {
    297 		warn("Can't stat `%s'", path);
    298 		return;
    299 	}
    300 
    301 #ifdef HAVE_FBIO
    302 	/*
    303 	 * We default to WSCONS.  If this is a frame buffer
    304 	 * device, check to see if it responds to the old
    305 	 * Sun-style fbio ioctls.  If so, switch to fbio mode.
    306 	 */
    307 	if (isfb && setvideo != FBIOSVIDEO) {
    308 		int onoff, fd;
    309 
    310 		if ((fd = open(path, O_RDWR, 0666)) == -1) {
    311 			warn("Can't open `%s'", path);
    312 			return;
    313 		}
    314 		if ((ioctl(fd, FBIOGVIDEO, &onoff)) == 0)
    315 			setvideo = FBIOSVIDEO;
    316 		(void)close(fd);
    317 	}
    318 #endif
    319 
    320 	/* Create the entry... */
    321 	dsp = malloc(sizeof(struct dev_stat));
    322 	if (dsp == NULL)
    323 		err(1, "Can't allocate memory for `%s'", path);
    324 	(void)memset(dsp, 0, sizeof(struct dev_stat));
    325 	dsp->ds_path = path;
    326 	dsp->ds_isfb = isfb;
    327 
    328 	/* ...and put it in the list. */
    329 	LIST_INSERT_HEAD(&ds_list, dsp, ds_link);
    330 }
    331 
    332 /* ARGSUSED */
    333 static void
    334 sighandler(int sig)
    335 {
    336 
    337 	/* Kill the pid file and re-enable the framebuffer before exit. */
    338 	change_state(videoon);
    339 	exit(0);
    340 }
    341 
    342 /*
    343  * Return 1 if we are a framebuffer in graphics mode or a framebuffer
    344  * where we cannot tell the mode. Return 0 if we are not a framebuffer
    345  * device, or a wscons framebuffer in text mode.
    346  */
    347 static int
    348 is_graphics_fb(struct dev_stat *dsp)
    349 {
    350 	int fd;
    351 	int state;
    352 
    353 	if (dsp->ds_isfb == 0)
    354 		return 0;
    355 
    356 	/* We can't tell if we are not a wscons device */
    357 	if (setvideo != WSDISPLAYIO_SVIDEO)
    358 		return 1;
    359 
    360 	if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
    361 		syslog(LOG_WARNING, "Cannot open `%s' (%m)", dsp->ds_path);
    362 		return 1;
    363 	}
    364 
    365 	if (ioctl(fd, WSDISPLAYIO_GMODE, &state) == -1) {
    366 		syslog(LOG_WARNING, "Cannot get mode on `%s' (%m)",
    367 		    dsp->ds_path);
    368 		/* We can't tell, so we say we are mapped */
    369 		state = WSDISPLAYIO_MODE_MAPPED;
    370 	}
    371 
    372 	(void)close(fd);
    373 
    374 	return state != WSDISPLAYIO_MODE_EMUL;
    375 }
    376 
    377 static void
    378 change_state(int state)
    379 {
    380 	struct dev_stat *dsp;
    381 	int fd;
    382 	int fail = 1;
    383 
    384 	LIST_FOREACH(dsp, &ds_list, ds_link) {
    385 		/* Don't change the state of non-framebuffers! */
    386 		if (dsp->ds_isfb == 0)
    387 			continue;
    388 		if ((fd = open(dsp->ds_path, O_RDWR, 0)) == -1) {
    389 			syslog(LOG_WARNING, "Can't open `%s' (%m)",
    390 			    dsp->ds_path);
    391 			continue;
    392 		}
    393 		if (ioctl(fd, setvideo, &state) == -1)
    394 			syslog(LOG_WARNING, "Can't set video on `%s' (%m)",
    395 			    dsp->ds_path);
    396 		else
    397 			fail = 0;
    398 		(void)close(fd);
    399 	}
    400 	if (fail) {
    401 		syslog(LOG_CRIT, "No frame buffer devices, exiting\n");
    402 		exit(1);
    403 	}
    404 }
    405 
    406 static void
    407 cvt_arg(char *arg, struct timespec *tvp)
    408 {
    409 	char *cp;
    410 	int seconds, nanoseconds, factor;
    411 	int period = 0;
    412 	factor = 1000000000;
    413 	nanoseconds = 0;
    414 	seconds = 0;
    415 
    416 	for (cp = arg; *cp != '\0'; ++cp) {
    417 		if (*cp == '.') {
    418 			if (period)
    419 				errx(1, "Invalid argument: %s", arg);
    420 			period = 1;
    421 			continue;
    422 		}
    423 
    424 		if (!isdigit((unsigned char)*cp))
    425 			errx(1, "Invalid argument: %s", arg);
    426 
    427 		if (period) {
    428 			if (factor > 1) {
    429 				nanoseconds = nanoseconds * 10 + (*cp - '0');
    430 				factor /= 10;
    431 			}
    432 		} else
    433 			seconds = (seconds * 10) + (*cp - '0');
    434 	}
    435 
    436 	tvp->tv_sec = seconds;
    437 	if (factor > 1)
    438 		nanoseconds *= factor;
    439 
    440 	tvp->tv_nsec = nanoseconds;
    441 }
    442 
    443 static void
    444 usage(void)
    445 {
    446 
    447 	(void)fprintf(stderr,
    448 	    "usage: %s [-k | -m] [-d inactivity-timeout] [-e wakeup-delay]\n"
    449 	    "\t\t[-f framebuffer] [-i input-device]\n",
    450 	    getprogname());
    451 	exit(1);
    452 }
    453