chiark / gitweb /
sysusers: fix uninitialized warning
[elogind.git] / src / readahead / 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 #if (__GNUC__ >= 4)
41 #  ifdef SD_EXPORT_SYMBOLS
42 /* Export symbols */
43 #    define _sd_export_ __attribute__ ((visibility("default")))
44 #  else
45 /* Don't export the symbols */
46 #    define _sd_export_ __attribute__ ((visibility("hidden")))
47 #  endif
48 #else
49 #  define _sd_export_
50 #endif
51
52 static int touch(const char *path) {
53
54 #if !defined(DISABLE_SYSTEMD) && defined(__linux__)
55         int fd;
56
57         mkdir("/run/systemd", 0755);
58         mkdir("/run/systemd/readahead", 0755);
59
60         fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0666);
61         if (fd < 0)
62                 return -errno;
63
64         for (;;) {
65                 if (close(fd) >= 0)
66                         break;
67
68                 if (errno != EINTR)
69                         return -errno;
70         }
71
72 #endif
73         return 0;
74 }
75
76 _sd_export_ int sd_readahead(const char *action) {
77
78         if (!action)
79                 return -EINVAL;
80
81         if (strcmp(action, "cancel") == 0)
82                 return touch("/run/systemd/readahead/cancel");
83         else if (strcmp(action, "done") == 0)
84                 return touch("/run/systemd/readahead/done");
85         else if (strcmp(action, "noreplay") == 0)
86                 return touch("/run/systemd/readahead/noreplay");
87
88         return -EINVAL;
89 }