Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*	$NetBSD: apmd.c,v 1.32 2011/11/25 12:51:27 joerg Exp $	*/

/*-
 * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by John Kohl.
 *
 * 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.
 *
 * 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 FOUNDATION 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.
 */

#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <fcntl.h>
#include <unistd.h>
#include <util.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <poll.h>
#include <machine/apmvar.h>
#include <err.h>
#include "pathnames.h"
#include "apm-proto.h"

#define TRUE 1
#define FALSE 0

#define POWER_STATUS_ACON	0x1
#define POWER_STATUS_LOWBATTNOW	0x2

static const char apmdev[] = _PATH_APM_CTLDEV;
static const char sockfile[] = _PATH_APM_SOCKET;

static int debug = 0;
static int verbose = 0;

__dead static void usage (void);
static int power_status (int fd, int force, struct apm_power_info *pinfo);
static int bind_socket (const char *sn, mode_t mode, uid_t uid, gid_t gid);
static enum apm_state handle_client(int sock_fd, int ctl_fd);
static void suspend(int ctl_fd);
static void stand_by(int ctl_fd);
static void resume(int ctl_fd);
__dead static void sigexit(int signo);
static void make_noise(int howmany);
static void do_etc_file(const char *file);
static void do_ac_state(int state);

static void
sigexit(int signo)
{
    exit(1);
}

static void
usage(void)
{
    fprintf(stderr,"usage: %s [-adlqsv] [-t seconds] [-S sockname]\n\t[-m sockmode] [-o sockowner:sockgroup] [-f devname]\n", getprogname());
    exit(1);
}


static int
power_status(int fd, int force, struct apm_power_info *pinfo)
{
    struct apm_power_info bstate;
    static struct apm_power_info last;
    int acon = 0;
    int lowbattnow = 0;

    memset(&bstate, 0, sizeof(bstate));
    if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) {
	/* various conditions under which we report status:  something changed
	   enough since last report, or asked to force a print */
	if (bstate.ac_state == APM_AC_ON)
	    acon = 1;
	if (bstate.battery_state != last.battery_state  &&
	    bstate.battery_state == APM_BATT_LOW)
		lowbattnow = 1;
	if (force || 
	    bstate.ac_state != last.ac_state ||
	    bstate.battery_state != last.battery_state ||
	    (bstate.minutes_left && bstate.minutes_left < 15) ||
	    abs(bstate.battery_life - last.battery_life) > 20) {
	    if (verbose) {
		if (bstate.minutes_left)
		    syslog(LOG_NOTICE,
		           "battery status: %s. external power status: %s. "
		           "estimated battery life %d%% (%d minutes)",
		           battstate(bstate.battery_state),
		           ac_state(bstate.ac_state), bstate.battery_life,
		           bstate.minutes_left);
		else
		    syslog(LOG_NOTICE,
		           "battery status: %s. external power status: %s. "
		           "estimated battery life %d%%",
		           battstate(bstate.battery_state),
		           ac_state(bstate.ac_state), bstate.battery_life);
	    }
	    last = bstate;
	}
	if (pinfo)
	    *pinfo = bstate;
    } else
	syslog(LOG_ERR, "cannot fetch power status: %m");
    return ((acon?POWER_STATUS_ACON:0) |
	(lowbattnow?POWER_STATUS_LOWBATTNOW:0));
}

static char *socketname;

static void sockunlink(void);

static void
sockunlink(void)
{
    if (socketname)
	(void) remove(socketname);
}

static int
bind_socket(const char *sockname, mode_t mode, uid_t uid, gid_t gid)
{
    int sock;
    struct sockaddr_un s_un;

    sock = socket(AF_LOCAL, SOCK_STREAM, 0);
    if (sock == -1)
	err(1, "cannot create local socket");

    s_un.sun_family = AF_LOCAL;
    strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
    s_un.sun_len = SUN_LEN(&s_un);
    /* remove it if present, we're moving in */
    (void) remove(sockname);
    if (bind(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1)
	err(1, "cannot create APM socket");
    if (chmod(sockname, mode) == -1 || chown(sockname, uid, gid) == -1)
	err(1, "cannot set socket mode/owner/group to %o/%d/%d",
	    mode, uid, gid);
    listen(sock, 1);
    socketname = strdup(sockname);
    atexit(sockunlink);
    return sock;
}

static enum apm_state
handle_client(int sock_fd, int ctl_fd)
{
    /* accept a handle from the client, process it, then clean up */
    int cli_fd;
    struct sockaddr_un from;
    socklen_t fromlen = sizeof(from);
    struct apm_command cmd;
    struct apm_reply reply;

    cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen);
    if (cli_fd == -1) {
	syslog(LOG_INFO, "client accept failure: %m");
	return NORMAL;
    }
    if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) {
	(void) close(cli_fd);
	syslog(LOG_INFO, "client size botch");
	return NORMAL;
    }
    if (cmd.vno != APMD_VNO) {
	close(cli_fd);			/* terminate client */
	/* no error message, just drop it. */
	return NORMAL;
    }
    power_status(ctl_fd, 0, &reply.batterystate);
    switch (cmd.action) {
    default:
	reply.newstate = NORMAL;
	break;
    case SUSPEND:
	reply.newstate = SUSPENDING;
	break;
    case STANDBY:
	reply.newstate = STANDING_BY;
	break;
    }	
    reply.vno = APMD_VNO;
    if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply)) {
	syslog(LOG_INFO, "client reply botch");
    }
    close(cli_fd);
    return reply.newstate;
}

static int speaker_ok = TRUE;

static void
make_noise(int howmany)
{
    int spkrfd;
    int trycnt;

    if (!speaker_ok)		/* don't bother after sticky errors */
	return;

    for (trycnt = 0; trycnt < 3; trycnt++) {
	spkrfd = open(_PATH_DEV_SPEAKER, O_WRONLY);
	if (spkrfd == -1) {
	    switch (errno) {
	    case EBUSY:
		usleep(500000);
		errno = EBUSY;
		continue;
	    case ENOENT:
	    case ENODEV:
	    case ENXIO:
	    case EPERM:
	    case EACCES:
		syslog(LOG_INFO,
		       "speaker device " _PATH_DEV_SPEAKER " unavailable: %m");
		speaker_ok = FALSE;
		return;
	    }
	} else
	    break;
    }
    if (spkrfd == -1) {
	syslog(LOG_WARNING, "cannot open " _PATH_DEV_SPEAKER ": %m");
	return;
    }
    syslog(LOG_DEBUG, "sending %d tones to speaker\n", howmany);
    write (spkrfd, "o4cc", 2 + howmany);
    close(spkrfd);
    return;
}


static void
suspend(int ctl_fd)
{
    do_etc_file(_PATH_APM_ETC_SUSPEND);
    sync();
    make_noise(2);
    sync();
    sync();
    sleep(1);
    ioctl(ctl_fd, APM_IOC_SUSPEND, 0);
}

static void
stand_by(int ctl_fd)
{
    do_etc_file(_PATH_APM_ETC_STANDBY);
    sync();
    make_noise(1);
    sync();
    sync();
    sleep(1);
    ioctl(ctl_fd, APM_IOC_STANDBY, 0);
}

#define TIMO (10*60)			/* 10 minutes */

static void
resume(int ctl_fd)
{
    do_etc_file(_PATH_APM_ETC_RESUME);
}

int
main(int argc, char *argv[])
{
    const char *fname = apmdev;
    int ctl_fd, sock_fd, ch, ready;
    int statonly = 0;
    struct pollfd set[2];
    struct apm_event_info apmevent;
    int suspends, standbys, resumes;
    int ac_is_off;
    int noacsleep = 0;
    int lowbattsleep = 0;
    mode_t mode = 0660;
    unsigned long timeout = TIMO;
    const char *sockname = sockfile;
    char *user, *group;
    char *scratch;
    uid_t uid = 2; /* operator */
    gid_t gid = 5; /* operator */
    struct passwd *pw;
    struct group *gr;

    while ((ch = getopt(argc, argv, "adlqsvf:t:S:m:o:")) != -1)
	switch(ch) {
	case 'q':
	    speaker_ok = FALSE;
	    break;
	case 'a':
	    noacsleep = 1;
	    break;
	case 'l':
	    lowbattsleep = 1;
	    break;
	case 'd':
	    debug = 1;
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case 'f':
	    fname = optarg;
	    break;
	case 'S':
	    sockname = optarg;
	    break;
	case 't':
	    timeout = strtoul(optarg, 0, 0);
	    if (timeout == 0)
		usage();
	    break;
	case 'm':
	    mode = strtoul(optarg, 0, 8);
	    if (mode == 0)
		usage();
	    break;
	case 'o':
	    /* (user):(group) */
	    user = optarg;
	    group = strchr(user, ':');
	    if (group)
		*group++ = '\0';
	    if (*user) {
		uid = strtoul(user, &scratch, 0);
		if (*scratch != '\0') {
		    pw = getpwnam(user);
		    if (pw)
			uid = pw->pw_uid;
		    else
			errx(1, "user name `%s' unknown", user);
		}
	    }
	    if (group && *group) {
		gid = strtoul(group, &scratch, 0);
		if (*scratch != '\0') {
		    gr = getgrnam(group);
		    if (gr)
			gid = gr->gr_gid;
		    else
			errx(1, "group name `%s' unknown", group);
		}
	    }
	    break;
	case 's':			/* status only */
	    statonly = 1;
	    break;
	case '?':

	default:
	    usage();
	}
    argc -= optind;
    argv += optind;
    if (debug) {
	openlog("apmd", 0, LOG_LOCAL1);
    } else {
	daemon(0, 0);
	openlog("apmd", 0, LOG_DAEMON);
	setlogmask(LOG_UPTO(LOG_NOTICE));
	pidfile(NULL);
    }
    if ((ctl_fd = open(fname, O_RDWR)) == -1) {
	syslog(LOG_ERR, "cannot open device file `%s'", fname);
	exit(1);
    } 

    if (statonly) {
        power_status(ctl_fd, 1, 0);
	exit(0);
    } else {
	struct apm_power_info pinfo;
	power_status(ctl_fd, 1, &pinfo);
	do_ac_state(pinfo.ac_state);
	ac_is_off = (pinfo.ac_state == APM_AC_OFF);
    }

    (void) signal(SIGTERM, sigexit);
    (void) signal(SIGHUP, sigexit);
    (void) signal(SIGINT, sigexit);
    (void) signal(SIGPIPE, SIG_IGN);


    sock_fd = bind_socket(sockname, mode, uid, gid);

    set[0].fd = ctl_fd;
    set[0].events = POLLIN;
    set[1].fd = sock_fd;
    set[1].events = POLLIN;

    
    for (errno = 0;
	 (ready = poll(set, 2, timeout * 1000)) >= 0 || errno == EINTR;
	 errno = 0) {
	if (errno == EINTR)
	    continue;
	if (ready == 0) {
		int status;
		/* wakeup for timeout: take status */
		status = power_status(ctl_fd, 0, 0);
		if (lowbattsleep && status&POWER_STATUS_LOWBATTNOW) {
			if (noacsleep && status&POWER_STATUS_ACON) {
				if (debug)
					syslog(LOG_DEBUG,
					    "not sleeping because "
					    "AC is connected");
			} else
				suspend(ctl_fd);
		}
	}
	if (set[0].revents & POLLIN) {
	    suspends = standbys = resumes = 0;
	    while (ioctl(ctl_fd, APM_IOC_NEXTEVENT, &apmevent) == 0) {
		if (debug)
		    syslog(LOG_DEBUG, "apmevent %04x index %d", apmevent.type,
		           apmevent.index);
		switch (apmevent.type) {
		case APM_SUSPEND_REQ:
		case APM_USER_SUSPEND_REQ:
		case APM_CRIT_SUSPEND_REQ:
		    suspends++;
		    break;
		case APM_BATTERY_LOW:
		    if (lowbattsleep)
			suspends++;
		    break;
		case APM_USER_STANDBY_REQ:
		case APM_STANDBY_REQ:
		    standbys++;
		    break;
#if 0
		case APM_CANCEL:
		    suspends = standbys = 0;
		    break;
#endif
		case APM_NORMAL_RESUME:
		case APM_CRIT_RESUME:
		case APM_SYS_STANDBY_RESUME:
		    resumes++;
		    break;
		case APM_POWER_CHANGE:
		{
		    struct apm_power_info pinfo;
		    power_status(ctl_fd, 0, &pinfo);
		    /* power status can change without ac status changing */
		    if (ac_is_off != (pinfo.ac_state == APM_AC_OFF)) {
		    	do_ac_state(pinfo.ac_state);
			ac_is_off = (pinfo.ac_state == APM_AC_OFF);
		    }
		    break;
		}
		default:
		    break;
		}
	    }
	    if ((standbys || suspends) && noacsleep &&
		(power_status(ctl_fd, 0, 0) & POWER_STATUS_ACON)) {
		if (debug)
		    syslog(LOG_DEBUG, "not sleeping because AC is connected");
	    } else if (suspends) {
		suspend(ctl_fd);
	    } else if (standbys) {
		stand_by(ctl_fd);
	    } else if (resumes) {
		resume(ctl_fd);
		if (verbose)
		    syslog(LOG_NOTICE, "system resumed from APM sleep");
	    }
	    ready--;
	}
	if (ready == 0)
	    continue;
	if (set[1].revents & POLLIN) {
	    switch (handle_client(sock_fd, ctl_fd)) {
	    case NORMAL:
		break;
	    case SUSPENDING:
		suspend(ctl_fd);
		break;
	    case STANDING_BY:
		stand_by(ctl_fd);
		break;
	    }
	}
    }
    syslog(LOG_ERR, "poll failed: %m");
    exit(1);
}

static void
do_etc_file(const char *file)
{
    pid_t pid;
    int status;
    const char *prog;

    /* If file doesn't exist, do nothing. */
    if (access(file, X_OK|R_OK)) {
	if (debug)
	    syslog(LOG_DEBUG, "do_etc_file(): cannot access file %s", file);
	return;
    }

    prog = strrchr(file, '/');
    if (prog)
	prog++;
    else
	prog = file;

    pid = fork();
    switch (pid) {
    case -1:
	syslog(LOG_ERR, "failed to fork(): %m");
	return;
    case 0:
	/* We are the child. */
	if (execl(file, prog, NULL) == -1)
		syslog(LOG_ERR, "could not execute \"%s\": %m", file);
	_exit(1);
	/* NOTREACHED */
    default:
	/* We are the parent. */
	wait4(pid, &status, 0, 0);
	if (WIFEXITED(status)) {
	    if (debug)
		syslog(LOG_DEBUG, "%s exited with status %d", file,
		       WEXITSTATUS(status));
	} else
	    syslog(LOG_ERR, "%s exited abnormally.", file);
	break;
    }
}

static void
do_ac_state(int state)
{
	switch (state) {
	case APM_AC_OFF:
		do_etc_file(_PATH_APM_ETC_BATTERY);
		break;
	case APM_AC_ON:
	case APM_AC_BACKUP:
		do_etc_file(_PATH_APM_ETC_LINE);
		break;
	default:
		/* Silently ignore */ ;
	}
}