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