chiark / gitweb /
manager: add log control via RT signals
[elogind.git] / src / sd-readahead.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   Copyright 2010 Lennart Poettering
5
6   Permission is hereby granted, free of charge, to any person
7   obtaining a copy of this software and associated documentation files
8   (the "Software"), to deal in the Software without restriction,
9   including without limitation the rights to use, copy, modify, merge,
10   publish, distribute, sublicense, and/or sell copies of the Software,
11   and to permit persons to whom the Software is furnished to do so,
12   subject to the following conditions:
13
14   The above copyright notice and this permission notice shall be
15   included in all copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   SOFTWARE.
25 ***/
26
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE
29 #endif
30
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <string.h>
37
38 #include "sd-readahead.h"
39
40 static int touch(const char *path) {
41
42 #if !defined(DISABLE_SYSTEMD) && defined(__linux__)
43         int fd;
44
45         mkdir("/run/systemd", 0755);
46         mkdir("/run/systemd/readahead", 0755);
47
48         if ((fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0666)) < 0)
49                 return -errno;
50
51         for (;;) {
52                 if (close(fd) >= 0)
53                         break;
54
55                 if (errno != -EINTR)
56                         return -errno;
57         }
58
59 #endif
60         return 0;
61 }
62
63 int sd_readahead(const char *action) {
64
65         if (!action)
66                 return -EINVAL;
67
68         if (strcmp(action, "cancel") == 0)
69                 return touch("/run/systemd/readahead/cancel");
70         else if (strcmp(action, "done") == 0)
71                 return touch("/run/systemd/readahead/done");
72         else if (strcmp(action, "noreplay") == 0)
73                 return touch("/run/systemd/readahead/noreplay");
74
75         return -EINVAL;
76 }