summaryrefslogtreecommitdiff
path: root/usr.sbin/screenblank/screenblank.c
blob: a62c236ddd515c9b681857711be92d621d5fd263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*	$OpenBSD: screenblank.c,v 1.11 2001/12/09 14:58:21 miod Exp $	*/
/*	$NetBSD: screenblank.c,v 1.2 1996/02/28 01:18:34 thorpej Exp $	*/

/*-
 * Copyright (c) 1996 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Screensaver daemon for the Sun 3 and SPARC.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <paths.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <syslog.h>

#include <machine/fbio.h>

#include "pathnames.h"

struct	dev_stat {
	LIST_ENTRY(dev_stat) ds_link;	/* linked list */
	char	*ds_path;		/* path to device */
	int	ds_isfb;		/* boolean; framebuffer? */
	time_t	ds_atime;		/* time device last accessed */
	time_t	ds_mtime;		/* time device last modified */
};
LIST_HEAD(ds_list, dev_stat) ds_list;

extern	char *__progname;

void add_dev __P((char *, int));
void change_state __P((int, int));
void cvt_arg __P((char *, struct timeval *));
void logpid __P((void));
void sighandler __P((int));
void usage __P((void));
void cleanup __P((int));

int
main(argc, argv)
	int argc;
	char **argv;
{
	struct dev_stat *dsp;
	struct timeval timo_on, timo_off, *tvp;
	struct sigaction sa;
	struct stat st;
	int ch, change, fflag = 0, kflag = 0, mflag = 0, state;

	LIST_INIT(&ds_list);

	/*
	 * Set the default timeouts: 10 minutes on, .25 seconds off.
	 */
	timo_on.tv_sec = 600;
	timo_on.tv_usec = 0;
	timo_off.tv_sec = 0;
	timo_off.tv_usec = 250000;

	while ((ch = getopt(argc, argv, "d:e:f:km")) != -1) {
		switch (ch) {
		case 'd':
			cvt_arg(optarg, &timo_on);
			break;

		case 'e':
			cvt_arg(optarg, &timo_off);
			break;

		case 'f':
			fflag = 1;
			add_dev(optarg, 1);
			break;

		case 'k':
			if (mflag || kflag)
				usage();
			kflag = 1;
			break;

		case 'm':
			if (kflag || mflag)
				usage();
			mflag = 1;
			break;

		default:
			usage();
		}
	}
	argc -= optind;
	if (argc)
		usage();

	/*
	 * Add the keyboard, mouse, and default framebuffer devices
	 * as necessary.  We _always_ check the console device.
	 */
	add_dev(_PATH_CONSOLE, 0);
	if (!kflag)
		add_dev(_PATH_KEYBOARD, 0);
	if (!mflag)
		add_dev(_PATH_MOUSE, 0);
	if (!fflag)
		add_dev(_PATH_FB, 1);

	/* Ensure that the framebuffer is on. */
	state = FBVIDEO_ON;
	change_state(state, 0);
	tvp = &timo_on;

	/*
	 * Make sure the framebuffer gets turned back on when we're
	 * killed.
	 */
	memset(&sa, 0, sizeof sa);
	sa.sa_handler = sighandler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_NOCLDSTOP;
	if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL) ||
	    sigaction(SIGHUP, &sa, NULL))
		err(1, "sigaction");

	/* Detach. */
	openlog(__progname, LOG_PID, LOG_DAEMON);
	if (daemon(0, 0))
		err(1, "daemon");
	logpid();

	/* Start the state machine. */
	for (;;) {
		change = 0;
		for (dsp = ds_list.lh_first; dsp != NULL;
		    dsp = dsp->ds_link.le_next) {
			/* Don't check framebuffers. */
			if (dsp->ds_isfb)
				continue;
			if (stat(dsp->ds_path, &st) < 0) {
				syslog(LOG_ERR, "stat(%s): %m",
				    dsp->ds_path);
				cleanup(0);
				exit(1);
			}
			if (st.st_atime > dsp->ds_atime) {
				change = 1;
				dsp->ds_atime = st.st_atime;
			}
			if (st.st_mtime > dsp->ds_mtime) {
				change = 1;
				dsp->ds_mtime = st.st_mtime;
			}
		}

		switch (state) {
		case FBVIDEO_ON:
			if (!change) {
				state = FBVIDEO_OFF;
				change_state(state, 0);
				tvp = &timo_off;
			}
			break;

		case FBVIDEO_OFF:
			if (change) {
				state = FBVIDEO_ON;
				change_state(state, 0);
				tvp = &timo_on;
			}
			break;
		}

		if (select(0, NULL, NULL, NULL, tvp) < 0) {
			syslog(LOG_ERR, "select: %m");
			cleanup(0);
			exit(1);
		}
	}
	/* NOTREACHED */
}

void
add_dev(path, isfb)
	char *path;
	int isfb;
{
	struct dev_stat *dsp1, *dsp2;

	/* Create the entry... */
	dsp1 = malloc(sizeof(struct dev_stat));
	if (dsp1 == NULL)
		errx(1, "can't allocate memory for %s", path);
	bzero(dsp1, sizeof(struct dev_stat));
	dsp1->ds_path = path;
	dsp1->ds_isfb = isfb;

	/* ...and put it in the list. */
	if (ds_list.lh_first == NULL) {
		LIST_INSERT_HEAD(&ds_list, dsp1, ds_link);
	} else {
		for (dsp2 = ds_list.lh_first; dsp2->ds_link.le_next != NULL;
		    dsp2 = dsp2->ds_link.le_next)
			/* Nothing. */ ;
		LIST_INSERT_AFTER(dsp2, dsp1, ds_link);
	}
}

/* ARGSUSED */
void
sighandler(sig)
	int sig;
{

	cleanup(1);
	_exit(0);
}

void
cleanup(sig)
	int sig;
{

	/* Kill the pid file and re-enable the framebuffer before exit. */
	unlink(_PATH_SCREENBLANKPID);
	change_state(FBVIDEO_ON, sig);
}

void
change_state(state, sig)
	int state;
	int sig;
{
	struct dev_stat *dsp;
	int fd;

	for (dsp = ds_list.lh_first; dsp != NULL; dsp = dsp->ds_link.le_next) {
		/* Don't change the state of non-framebuffers! */
		if (dsp->ds_isfb == 0)
			continue;
		if ((fd = open(dsp->ds_path, O_RDWR, 0)) < 0) {
			if (errno == ENXIO) {
				if (sig)
					_exit(1);
				else {
					syslog(LOG_ERR, "open(%s): %m",
					    dsp->ds_path);
					exit(1);
				}
			}
			if (!sig)
				syslog(LOG_WARNING, "open: %s", dsp->ds_path);
			continue;
		}
		if (ioctl(fd, FBIOSVIDEO, &state) < 0) {
			if (!sig)
				syslog(LOG_WARNING, "ioctl: %s", dsp->ds_path);
		}
		(void)close(fd);
	}
}

void
cvt_arg(arg, tvp)
	char *arg;
	struct timeval *tvp;
{
	char *cp;
	double seconds = 0.0, exponent = -1.0;
	int period = 0;

	for (cp = arg; *cp != '\0'; ++cp) {
		if (*cp == '.') {
			if (period)
				errx(1, "invalid argument: %s", arg);
			period = 1;
			continue;
		}

		if (!isdigit(*cp))
			errx(1, "invalid argument: %s", arg);

		if (period) {
			seconds = seconds + ((*cp - '0') * pow(10.0, exponent));
			exponent -= 1.0;
		} else
			seconds = (seconds * 10.0) + (*cp - '0');
	}

	tvp->tv_sec = (long)seconds;
	tvp->tv_usec = (long)((seconds - tvp->tv_sec) * 1000000);
}

void
logpid()
{
	FILE *fp;

	if ((fp = fopen(_PATH_SCREENBLANKPID, "w")) != NULL) {
		fprintf(fp, "%u\n", getpid());
		(void)fclose(fp);
	}
}

void
usage()
{

	fprintf(stderr, "usage: %s [-k | -m] [-d timeout] [-e timeout] %s\n",
	    __progname, "[-f framebuffer]");
	exit(1);
}