]> git.neil.brown.name Git - susman.git/blob - request_suspend.c
wakealarmd: cope with delta between system time and RTC time.
[susman.git] / request_suspend.c
1 /* Request suspend
2  * Create the suspend-request file, then wait for it to be deleted.
3  *
4  * Copyright (C) 2011 Neil Brown <neilb@suse.de>
5  *
6  *    This program is free software; you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation; either version 2 of the License, or
9  *    (at your option) any later version.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License along
17  *    with this program; if not, write to the Free Software Foundation, Inc.,
18  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 #define _GNU_SOURCE
21
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <signal.h>
26
27 static void catch(int sig)
28 {
29         return;
30 }
31
32 main(int argc, char *argv[])
33 {
34         int dirfd = open("/run/suspend", O_RDONLY);
35         int fd_request = open("/run/suspend/request",
36                               O_RDWR|O_CREAT, 0640);
37         int fd_watching = open("/run/suspend/watching", O_RDONLY);
38         if (fd_request < 0)
39                 exit(2);
40
41
42         if (dirfd < 0)
43                 exit(2);
44         /* Wait for unlink */
45         while (1) {
46                 int fd;
47                 struct stat stat;
48                 sigset_t set, oldset;
49                 sigemptyset(&set);
50                 sigaddset(&set, SIGIO);
51
52                 sigprocmask(SIG_BLOCK, &set, &oldset);
53                 signal(SIGIO, catch);
54
55                 fcntl(dirfd, F_NOTIFY, DN_DELETE);
56
57                 if (fstat(fd_request, &stat) != 0
58                     || stat.st_nlink == 0) {
59                         struct stat s1, s2;
60                         int fd_watching2 = open("/run/suspend/watching",
61                                                 O_RDONLY);
62                         if (fd_watching < 0 ||
63                             fd_watching2 < 0 ||
64                             fstat(fd_watching, &s1) < 0 ||
65                             fstat(fd_watching2, &s2) < 0)
66                                 /* something strange */
67                                 exit(2);
68                         if (s1.st_ino == s2.st_ino)
69                                 /* Didn't suspend - someone must be
70                                  * blocking suspend
71                                  */
72                                 exit(1);
73                         exit(0);
74                 }
75
76                 sigsuspend(&oldset);
77                 sigprocmask(SIG_UNBLOCK, &set, &oldset);
78         }
79 }