Allows to compile without libsystemd
[src/app-framework-main.git] / src / utils-systemd.c
1 /*
2  Copyright 2017 IoT.bzh
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 */
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #ifndef NO_LIBSYSTEMD
31 # include <systemd/sd-bus.h>
32 # include <systemd/sd-bus-protocol.h>
33 #else
34   struct sd_bus;
35 # define sd_bus_default_user(p)   ((*(p)=NULL),(-ENOTSUP))
36 # define sd_bus_default_system(p) ((*(p)=NULL),(-ENOTSUP))
37 # define sd_bus_call_method(...)  (-ENOTSUP)
38 #endif
39
40 #include "utils-systemd.h"
41
42 #if !defined(SYSTEMD_UNITS_ROOT)
43 # define SYSTEMD_UNITS_ROOT "/usr/local/lib/systemd"
44 #endif
45
46 static const char sdb_path[] = "/org/freedesktop/systemd1";
47 static const char sdb_destination[] = "org.freedesktop.systemd1";
48 static const char sdbi_manager[] = "org.freedesktop.systemd1.Manager";
49 static const char sdbm_reload[] = "Reload";
50
51 static struct sd_bus *sysbus;
52 static struct sd_bus *usrbus;
53
54 /*
55  * Translate systemd errors to errno errors
56  */
57 static int seterrno(int value)
58 {
59         errno = value;
60         return -1;
61 }
62
63 static int sderr2errno(int rc)
64 {
65         return rc < 0 ? seterrno(-rc) : rc;
66 }
67
68 static int errno2sderr(int rc)
69 {
70         return rc < 0 ? -errno : rc;
71 }
72
73 /* Returns in 'ret' either the system bus (if isuser==0)
74  * or the user bus (if isuser!=0).
75  * Returns 0 in case of success or -1 in case of error
76  */
77 static int get_bus(int isuser, struct sd_bus **ret)
78 {
79         int rc;
80         struct sd_bus *bus;
81
82         bus = isuser ? usrbus : sysbus;
83         if (bus) {
84                 *ret = bus;
85                 rc = 0;
86         } else if (isuser) {
87                 rc = sd_bus_default_user(ret);
88                 if (!rc)
89                         usrbus = *ret;
90         } else {
91                 rc = sd_bus_default_system(ret);
92                 if (!rc)
93                         sysbus = *ret;
94         }
95         return sderr2errno(rc);
96 }
97 static int check_snprintf_result(int rc, size_t buflen)
98 {
99         return (rc >= 0 && (size_t)rc >= buflen) ? seterrno(ENAMETOOLONG) : rc;
100 }
101
102 int systemd_get_units_dir(char *path, size_t pathlen, int isuser)
103 {
104         int rc = snprintf(path, pathlen, "%s/%s", 
105                         SYSTEMD_UNITS_ROOT,
106                         isuser ? "user" : "system");
107
108         return check_snprintf_result(rc, pathlen);
109 }
110
111 int systemd_get_unit_path(char *path, size_t pathlen, int isuser, const char *unit, const char *uext)
112 {
113         int rc = snprintf(path, pathlen, "%s/%s/%s.%s", 
114                         SYSTEMD_UNITS_ROOT,
115                         isuser ? "user" : "system",
116                         unit,
117                         uext);
118
119         return check_snprintf_result(rc, pathlen);
120 }
121
122 int systemd_get_wants_path(char *path, size_t pathlen, int isuser, const char *wanter, const char *unit, const char *uext)
123 {
124         int rc = snprintf(path, pathlen, "%s/%s/%s.wants/%s.%s", 
125                         SYSTEMD_UNITS_ROOT,
126                         isuser ? "user" : "system",
127                         wanter,
128                         unit,
129                         uext);
130
131         return check_snprintf_result(rc, pathlen);
132 }
133
134 int systemd_get_wants_target(char *path, size_t pathlen, const char *unit, const char *uext)
135 {
136         int rc = snprintf(path, pathlen, "../%s.%s", unit, uext);
137
138         return check_snprintf_result(rc, pathlen);
139 }
140
141 int systemd_daemon_reload(int isuser)
142 {
143         int rc;
144         struct sd_bus *bus;
145
146         rc = get_bus(isuser, &bus);
147         if (!rc) {
148                 /* TODO: asynchronous bind... */
149                 /* TODO: more diagnostic... */
150                 rc = sd_bus_call_method(bus, sdb_destination, sdb_path, sdbi_manager, sdbm_reload, NULL, NULL, NULL);
151         }
152         return rc;
153 }
154
155 int systemd_unit_list(int isuser, int (*callback)(void *closure, const char *name, const char *path, int isuser), void *closure)
156 {
157         DIR *dir;
158         char path[PATH_MAX + 1];
159         struct dirent *dent;
160         int rc, isfile;
161         size_t offset, len;
162         struct stat st;
163
164         /* get the path */
165         rc = systemd_get_units_dir(path, sizeof path - 1, isuser);
166         if (rc < 0)
167                 return rc;
168         offset = (size_t)rc;
169
170         /* open the directory */
171         dir = opendir(path);
172         if (!dir)
173                 return -1;
174
175         /* prepare path */
176         path[offset++] = '/';
177
178         /* read the directory */
179         for(;;) {
180                 /* get next entry */
181                 errno = 0;
182                 dent = readdir(dir);
183                 if (dent == NULL) {
184                         /* end or error */
185                         rc = (!errno) - 1;
186                         break;
187                 }
188
189                 /* is a file? */
190                 if (dent->d_type == DT_REG)
191                         isfile = 1;
192                 else if (dent->d_type != DT_UNKNOWN)
193                         isfile = 0;
194                 else {
195                         rc = fstatat(dirfd(dir), dent->d_name, &st, AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT);
196                         if (rc < 0)
197                                 break;
198                         isfile = S_ISREG(st.st_mode);
199                 }
200
201                 /* calls the callback if is a file */
202                 if (isfile) {
203                         len = strlen(dent->d_name);
204                         if (offset + len >= sizeof path) {
205                                 rc = seterrno(ENAMETOOLONG);
206                                 break;
207                         }
208                         memcpy(&path[offset], dent->d_name, 1 + len);
209                         rc = callback(closure, &path[offset], path, isuser);
210                         if (rc)
211                                 break;
212                 }
213         }
214         closedir(dir);
215         return rc;
216 }
217
218 int systemd_unit_list_all(int (*callback)(void *closure, const char *name, const char *path, int isuser), void *closure)
219 {
220         return systemd_unit_list(1, callback, closure) ? : systemd_unit_list(0, callback, closure);
221 }
222