utils-systemd: introduction of systemd interface
[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 int systemd_get_unit_path(char *path, size_t pathlen, int isuser, const char *unit, const char *uext)
37 {
38         int rc = snprintf(path, pathlen, "%s/%s/%s.%s", 
39                         SYSTEMD_UNITS_ROOT,
40                         isuser ? "user" : "system",
41                         unit,
42                         uext);
43
44         if (rc >= 0 && (size_t)rc >= pathlen) {
45                 errno = ENAMETOOLONG;
46                 rc = -1;
47         }
48         return rc;
49 }
50
51 int systemd_get_wants_path(char *path, size_t pathlen, int isuser, const char *wanter, const char *unit, const char *uext)
52 {
53         int rc = snprintf(path, pathlen, "%s/%s/%s.wants/%s.%s", 
54                         SYSTEMD_UNITS_ROOT,
55                         isuser ? "user" : "system",
56                         wanter,
57                         unit,
58                         uext);
59
60         if (rc >= 0 && (size_t)rc >= pathlen) {
61                 errno = ENAMETOOLONG;
62                 rc = -1;
63         }
64         return rc;
65 }
66
67 int systemd_get_wants_target(char *path, size_t pathlen, const char *unit, const char *uext)
68 {
69         int rc = snprintf(path, pathlen, "../%s.%s", unit, uext);
70
71         if (rc >= 0 && (size_t)rc >= pathlen) {
72                 errno = ENAMETOOLONG;
73                 rc = -1;
74         }
75         return rc;
76 }
77