]> git.neil.brown.name Git - susman.git/blob - suspend_block.c
lsusd: allow clients to abort this suspend cycle.
[susman.git] / suspend_block.c
1 /*
2  * Library routine to block and re-enable suspend.
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 #include <unistd.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include "libsus.h"
25
26 int suspend_open()
27 {
28         return open("/var/run/suspend/disabled", O_RDONLY|O_CLOEXEC);
29 }
30
31 int suspend_block(int handle)
32 {
33         if (handle < 0)
34                 handle = suspend_open();
35         if (handle < 0)
36                 return handle;
37
38         flock(handle, LOCK_SH);
39         return handle;
40 }
41
42 void suspend_allow(int handle)
43 {
44         flock(handle, LOCK_UN);
45 }
46
47 int suspend_close(int handle)
48 {
49         if (handle >= 0)
50                 close(handle);
51 }
52
53 void suspend_abort(int handle)
54 {
55         int h = handle;
56         char c;
57         if (handle < 0)
58                 h = suspend_open();
59         read(h, &c, 1);
60         if (handle < 0)
61                 suspend_close(h);
62 }