+/**************** SPECIALIZED PART *****************************/
+
+static int check_unit_desc(const struct unitdesc *desc, int tells)
+{
+ if (desc->scope != unitscope_unknown && desc->type != unittype_unknown && desc->name != NULL)
+ return 0;
+
+ 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;
+ 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 get_wants_path(char *path, size_t pathlen, const struct unitdesc *desc)
+{
+ 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 generatedesc *desc)
+{
+ int rc, rc2;
+ int i;
+ char path[PATH_MAX];
+ const struct unitdesc *u;
+
+ rc = 0;
+ for (i = 0 ; i < desc->nunits ; i++) {
+ u = &desc->units[i];
+ rc2 = check_unit_desc(u, 0);
+ if (rc2 == 0) {
+ rc2 = get_unit_path(path, sizeof path, u);
+ if (rc2 >= 0) {
+ rc2 = unlink(path);
+ }
+ if (rc2 < 0 && rc == 0)
+ rc = rc2;
+ if (u->wanted_by != NULL) {
+ rc2 = get_wants_path(path, sizeof path, u);
+ if (rc2 >= 0)
+ rc2 = unlink(path);
+ }
+ }
+ if (rc2 < 0 && rc == 0)
+ rc = rc2;
+ }
+ return rc;
+}
+
+static int do_install_units(void *closure, const struct generatedesc *desc)
+{
+ int rc;
+ int i;
+ char path[PATH_MAX + 1], target[PATH_MAX + 1];
+ const struct unitdesc *u;
+
+ i = 0;
+ while (i < desc->nunits) {
+ u = &desc->units[i];
+ rc = check_unit_desc(u, 1);
+ if (!rc) {
+ rc = get_unit_path(path, sizeof path, u);
+ if (rc >= 0) {
+ rc = putfile(path, u->content, u->content_length);
+ if (rc >= 0 && u->wanted_by != NULL) {
+ rc = get_wants_path(path, sizeof path, u);
+ if (rc >= 0) {
+ rc = get_wants_target(target, sizeof target, u);
+ if (rc >= 0) {
+ unlink(path); /* TODO? check? */
+ rc = symlink(target, path);
+ }
+ }
+ }
+ i++;
+ }
+ }
+ if (rc < 0)
+ goto error;
+ }
+ return 0;
+error:
+ i = errno;
+ do_uninstall_units(closure, desc);
+ errno = i;
+ return rc;
+}
+
+static int do_install_uninstall(
+ struct wgt_info *ifo,
+ const struct unitconf *conf,
+ int (*doer)(void *, const struct generatedesc *)
+)
+{
+ int rc;
+ struct json_object *jdesc;
+
+ jdesc = wgt_info_to_json(ifo);
+ if (!jdesc)
+ rc = -1;
+ else {
+ rc = unit_generator_process(jdesc, conf, doer, NULL);
+ json_object_put(jdesc);
+ }
+ return rc;
+}
+
+int unit_install(struct wgt_info *ifo, const struct unitconf *conf)
+{
+ return do_install_uninstall(ifo, conf, do_install_units);
+}
+
+int unit_uninstall(struct wgt_info *ifo, const struct unitconf *conf)
+{
+ return do_install_uninstall(ifo, conf, do_uninstall_units);
+}
+