binutils: update to 2.43.1
[openadk.git] / package / alix-switch / src / alix-switchd.c
blob78b52f19cb93d84d50d25a66e27e8d238458cab3
1 /*
2 * alix-switchd.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <signal.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <syslog.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <sys/io.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
26 #define SCRIPT "/etc/alix-switch"
27 #define GPIOBASE 0x6100
29 typedef void (*sighandler_t)(int);
31 static sighandler_t handle_signal (int sig_nr, sighandler_t signalhandler) {
33 struct sigaction neu_sig, alt_sig;
35 neu_sig.sa_handler = signalhandler;
36 sigemptyset(&neu_sig.sa_mask);
37 neu_sig.sa_flags = SA_RESTART;
38 if (sigaction (sig_nr, &neu_sig, &alt_sig) < 0)
39 return SIG_ERR;
41 return alt_sig.sa_handler;
44 static void start_daemon (void) {
46 int i;
47 pid_t pid, sid;
49 handle_signal(SIGHUP, SIG_IGN);
50 if ((pid = fork ()) != 0)
51 exit(EXIT_FAILURE);
52 umask(0);
53 if ((sid = setsid()) < 0)
54 exit(EXIT_FAILURE);
55 chdir("/");
56 for (i = sysconf(_SC_OPEN_MAX); i > 0; i--)
57 close(i);
61 int main(int argc, char *argv[]) {
63 int i;
64 unsigned long bPort = 0;
65 struct timespec sleep;
66 int bDaemon = 0, bSwitch = 0, bState = 0;
68 for(i = 1; i < argc; i++) {
69 if (!strcasecmp(argv[i], "-d") || !strcasecmp(argv[i], "--daemon")) {
70 bDaemon = 1;
71 } else {
72 printf( "\nusage: %s [-d | --daemon]\n", argv[0]);
73 exit(EXIT_FAILURE);
77 if (iopl(3)) {
78 fprintf( stderr, "Could not set I/O permissions to level 3\n");
79 exit(EXIT_FAILURE);
82 if (bDaemon)
83 start_daemon();
85 sleep.tv_sec = 0;
86 sleep.tv_nsec = 50000000;
88 while(1) {
89 bPort = inl(GPIOBASE + 0xB0);
90 if ((bPort & 0x100) == 0)
91 bState = 1;
92 else
93 bState = 0;
95 if (bState && !bSwitch)
96 system(SCRIPT " on");
98 bSwitch = bState;
99 nanosleep(&sleep, NULL);
102 if (iopl(0)) {
103 fprintf(stderr, "Could not set I/O permissions to level 0");
104 exit(EXIT_FAILURE);
107 return EXIT_SUCCESS;