#include "wgtpkg-mustach.h"
#include "utils-json.h"
#include "wgt-json.h"
+#include "utils-systemd.h"
#include "wgtpkg-unit.h"
-#if !defined(SYSTEMD_UNITS_ROOT)
-# define SYSTEMD_UNITS_ROOT "/usr/local/lib/systemd"
-#endif
-
#if 0
#include <ctype.h>
#else
*/
static int process_one_unit(char *spec, struct unitdesc *desc)
{
- char *nsoc, *nsrv, *name;
- int isuser, issystem, issock, isserv;
+ char *nsoc, *nsrv, *name, *wanted;
+ int isuser, issystem, issock, isserv, iswanted;
size_t len;
/* finds the configuration directive of the unit */
issystem = !!offset(spec, "%systemd-unit system\n", NULL);
issock = !!offset(spec, "%systemd-unit socket ", &nsoc);
isserv = !!offset(spec, "%systemd-unit service ", &nsrv);
+ iswanted = !!offset(spec, "%systemd-unit wanted-by ", &wanted);
/* check the unit scope */
if ((isuser + issystem) == 1) {
desc->name_length = 0;
}
+ if (iswanted) {
+ len = (size_t)(strchrnul(wanted, '\n') - wanted);
+ desc->wanted_by = strndup(wanted, len);
+ desc->wanted_by_length = len;
+ } else {
+ desc->wanted_by = NULL;
+ desc->wanted_by_length = 0;
+ }
+
desc->content = spec;
desc->content_length = pack(spec, '%');
rc = process(closure, descs, n);
/* cleanup and frees */
- while(n)
+ while(n) {
free((char *)(descs[--n].name));
+ free((char *)(descs[n].wanted_by));
+ }
free(descs);
return rc;
/**************** SPECIALIZED PART *****************************/
-
-static int get_unit_path(char *path, size_t pathlen, const struct unitdesc *desc)
+static int check_unit_desc(const struct unitdesc *desc, int tells)
{
- int rc;
+ if (desc->scope != unitscope_unknown && desc->type != unittype_unknown && desc->name != NULL)
+ return 0;
- if (desc->scope == unitscope_unknown || desc->type == unittype_unknown || desc->name == NULL) {
+ if (tells) {
if (desc->scope == unitscope_unknown)
ERROR("unit of unknown scope");
if (desc->type == unittype_unknown)
ERROR("unit of unknown type");
if (desc->name == NULL)
ERROR("unit of unknown name");
- errno = EINVAL;
- rc = -1;
- }
- else {
- rc = snprintf(path, pathlen, "%s/%s/%s.%s",
- SYSTEMD_UNITS_ROOT,
- desc->scope == unitscope_system ? "system" : "user",
- desc->name,
- desc->type == unittype_socket ? "socket" : "service");
-
- if (rc >= 0 && (size_t)rc >= pathlen) {
- ERROR("can't set the unit name");
- errno = EINVAL;
- rc = -1;
- }
}
+ errno = EINVAL;
+ return -1;
+}
+
+static int get_unit_path(char *path, size_t pathlen, const struct unitdesc *desc)
+{
+ int rc = systemd_get_unit_path(
+ path, pathlen, desc->scope == unitscope_user,
+ desc->name, desc->type == unittype_socket ? "socket" : "service");
+
+ if (rc < 0)
+ ERROR("can't get the unit path for %s", desc->name);
+
return rc;
}
-static int do_install_units(void *closure, const struct unitdesc descs[], unsigned count)
+static int get_wants_path(char *path, size_t pathlen, const struct unitdesc *desc)
{
- int rc;
+ int rc = systemd_get_wants_path(
+ path, pathlen, desc->scope == unitscope_user, desc->wanted_by,
+ desc->name, desc->type == unittype_socket ? "socket" : "service");
+
+ if (rc < 0)
+ ERROR("can't get the wants path for %s and %s", desc->name, desc->wanted_by);
+
+ return rc;
+}
+
+static int get_wants_target(char *path, size_t pathlen, const struct unitdesc *desc)
+{
+ int rc = systemd_get_wants_target(
+ path, pathlen,
+ desc->name, desc->type == unittype_socket ? "socket" : "service");
+
+ if (rc < 0)
+ ERROR("can't get the wants target for %s", desc->name);
+
+ return rc;
+}
+
+static int do_uninstall_units(void *closure, const struct unitdesc descs[], unsigned count)
+{
+ int rc, rc2;
unsigned i;
- char path[PATH_MAX + 1];
+ char path[PATH_MAX];
for (i = 0 ; i < count ; i++) {
- rc = get_unit_path(path, sizeof path, &descs[i]);
- if (rc >= 0) {
- rc = putfile(path, descs[i].content, descs[i].content_length);
+ rc = check_unit_desc(&descs[i], 0);
+ if (rc == 0) {
+ rc = get_unit_path(path, sizeof path, &descs[i]);
+ if (rc >= 0) {
+ rc = unlink(path);
+ }
+ if (descs[i].wanted_by != NULL) {
+ rc2 = get_wants_path(path, sizeof path, &descs[i]);
+ if (rc2 >= 0)
+ rc2 = unlink(path);
+ rc = rc < 0 ? rc : rc2;
+ }
}
}
return 0;
}
-static int do_uninstall_units(void *closure, const struct unitdesc descs[], unsigned count)
+static int do_install_units(void *closure, const struct unitdesc descs[], unsigned count)
{
int rc;
unsigned i;
- char path[PATH_MAX];
-
- for (i = 0 ; i < count ; i++) {
- rc = get_unit_path(path, sizeof path, &descs[i]);
- if (rc >= 0) {
- rc = unlink(path);
+ char path[PATH_MAX + 1], target[PATH_MAX + 1];
+
+ i = 0;
+ while (i < count) {
+ rc = check_unit_desc(&descs[i], 1);
+ if (!rc) {
+ rc = get_unit_path(path, sizeof path, &descs[i]);
+ if (rc >= 0) {
+ rc = putfile(path, descs[i].content, descs[i].content_length);
+ if (descs[i].wanted_by != NULL) {
+ rc = get_wants_path(path, sizeof path, &descs[i]);
+ if (rc >= 0) {
+ rc = get_wants_target(target, sizeof target, &descs[i]);
+ if (rc >= 0) {
+ unlink(path); /* TODO? check? */
+ rc = symlink(target, path);
+ }
+ }
+ }
+ i++;
+ }
+ }
+ if (rc < 0) {
+ do_uninstall_units(closure, descs, i);
+ return rc;
}
}
return 0;