62930ed708e281a4c7aa38bc9bbc164ce4f59b45
[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 <string.h>
24 #include <poll.h>
25 #include <assert.h>
26
27 #include <systemd/sd-bus.h>
28 #include <systemd/sd-bus-protocol.h>
29
30 #include "utils-systemd.h"
31
32 #if !defined(SYSTEMD_UNITS_ROOT)
33 # define SYSTEMD_UNITS_ROOT "/usr/local/lib/systemd"
34 #endif
35
36 static const char sdb_path[] = "/org/freedesktop/systemd1";
37 static const char sdb_destination[] = "org.freedesktop.systemd1";
38 static const char sdbi_manager[] = "org.freedesktop.systemd1.Manager";
39 static const char sdbm_reload[] = "Reload";
40
41 static struct sd_bus *sysbus;
42 static struct sd_bus *usrbus;
43
44 static int get_bus(int isuser, struct sd_bus **ret)
45 {
46         int rc;
47         struct sd_bus *bus;
48
49         bus = isuser ? usrbus : sysbus;
50         if (bus) {
51                 *ret = bus;
52                 rc = 0;
53         } else if (isuser) {
54                 rc = sd_bus_default_user(ret);
55                 if (!rc)
56                         usrbus = *ret;
57         } else {
58                 rc = sd_bus_default_system(ret);
59                 if (!rc)
60                         sysbus = *ret;
61         }
62         return rc;
63 }
64
65 int systemd_get_unit_path(char *path, size_t pathlen, int isuser, const char *unit, const char *uext)
66 {
67         int rc = snprintf(path, pathlen, "%s/%s/%s.%s", 
68                         SYSTEMD_UNITS_ROOT,
69                         isuser ? "user" : "system",
70                         unit,
71                         uext);
72
73         if (rc >= 0 && (size_t)rc >= pathlen) {
74                 errno = ENAMETOOLONG;
75                 rc = -1;
76         }
77         return rc;
78 }
79
80 int systemd_get_wants_path(char *path, size_t pathlen, int isuser, const char *wanter, const char *unit, const char *uext)
81 {
82         int rc = snprintf(path, pathlen, "%s/%s/%s.wants/%s.%s", 
83                         SYSTEMD_UNITS_ROOT,
84                         isuser ? "user" : "system",
85                         wanter,
86                         unit,
87                         uext);
88
89         if (rc >= 0 && (size_t)rc >= pathlen) {
90                 errno = ENAMETOOLONG;
91                 rc = -1;
92         }
93         return rc;
94 }
95
96 int systemd_get_wants_target(char *path, size_t pathlen, const char *unit, const char *uext)
97 {
98         int rc = snprintf(path, pathlen, "../%s.%s", unit, uext);
99
100         if (rc >= 0 && (size_t)rc >= pathlen) {
101                 errno = ENAMETOOLONG;
102                 rc = -1;
103         }
104         return rc;
105 }
106
107 int systemd_daemon_reload(int isuser)
108 {
109         int rc;
110         struct sd_bus *bus;
111
112         rc = get_bus(isuser, &bus);
113         if (!rc) {
114                 rc = sd_bus_call_method(bus, sdb_destination, sdb_path, sdbi_manager, sdbm_reload, NULL, NULL, NULL);
115         }
116         return rc;
117 }
118