Skip to content

Instantly share code, notes, and snippets.

@hallyn
Created April 10, 2022 14:52
Show Gist options
  • Save hallyn/b45e00ca41c6a4ee7cb61387022cd8da to your computer and use it in GitHub Desktop.
Save hallyn/b45e00ca41c6a4ee7cb61387022cd8da to your computer and use it in GitHub Desktop.
Attempt to use systemd transient units through dbus
Several problems. First, after I create the scope, it never goes away so I can't recreate it:
```
serge@jerom ~/test/sdbus$ ./a
currently I am in 0::/user.slice/user-1000.slice/session-136.scope
Queued service job as /org/freedesktop/systemd1/job/7212.
I am 1429460
currently I am in 0::/user.slice/user-1000.slice/session-136.scope
serge@jerom ~/test/sdbus$ ./a
currently I am in 0::/user.slice/user-1000.slice/session-136.scope
Failed sending message: Unit lxctest7.scope already exists.
currently I am in 0::/user.slice/user-1000.slice/session-136.scope
```
But I can't set RemainAfterExit. If I leave that in the properties in client.c, I get:
```
serge@jerom ~/test/sdbus$ ./a
currently I am in 0::/user.slice/user-1000.slice/session-136.scope
Failed sending message: Cannot set property RemainAfterExit, or unknown property.
currently I am in 0::/user.slice/user-1000.slice/session-136.scope
```
If I try to set it after that fact through busctl, I get:
```
serge@jerom ~$ busctl --user call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager GetUnit s lxctest.scope
o "/org/freedesktop/systemd1/unit/lxctest_2escope"
serge@jerom ~$ busctl --user call org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager SetUnitProperties "sba(sv)" lxctest.scope 1 1 RemainAfterExit b 0
Call failed: Cannot set property RemainAfterExit, or unknown property.
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <systemd/sd-bus.h>
void print_current_cgroup(void)
{
FILE *f;
char *line = NULL;
size_t len = 0;
f = fopen("/proc/self/cgroup", "r");
if (!f)
exit(1);
while (getline(&line, &len, f) != -1) {
printf("currently I am in %s\n", line);
}
fclose(f);
free(line);
}
/*
* from https://www.man7.org/linux/man-pages/man5/org.freedesktop.systemd1.5.html :
* StartTransientUnit(in s name,
* in s mode,
* in a(sv) properties,
* in a(sa(sv)) aux,
* out o job);
*/
int enter_new_cgroup()
{
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *reply = NULL, *m = NULL;
sd_bus *bus = NULL;
const char *path;
int r;
pid_t pid = getpid();
/* Connect to the system bus */
r = sd_bus_open_user(&bus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
}
#define DESTINATION "org.freedesktop.systemd1"
#define PATH "/org/freedesktop/systemd1"
#define INTERFACE "org.freedesktop.systemd1.Manager"
#define MEMBER "StartTransientUnit"
r = sd_bus_message_new_method_call(bus, &m,
DESTINATION, PATH, INTERFACE, MEMBER);
if (r < 0) {
fprintf(stderr, "Failed creating bus message\n");
goto finish;
}
char *scope = "lxctest7.scope";
r = sd_bus_message_append(m, "ss", scope, "replace");
if (r < 0) {
fprintf(stderr, "Failed setting scope name\n");
goto finish;
}
r = sd_bus_message_open_container(m, 'a', "(sv)");
if (r < 0) {
fprintf(stderr, "Failed allocating properties\n");
goto finish;
}
#if 0
r = sd_bus_message_append(m, "(sv)(sv)(sv)",
"PIDs", "au", 1, pid,
"Delegate", "b", 1,
"RemainAfterExit", "b", false);
#else
r = sd_bus_message_append(m, "(sv)(sv)",
"PIDs", "au", 1, pid,
"Delegate", "b", 1);
#endif
if (r < 0) {
fprintf(stderr, "Failed setting delegation\n");
goto finish;
}
r = sd_bus_message_close_container(m);
if (r < 0) {
fprintf(stderr, "Failed closing properties\n");
goto finish;
}
r = sd_bus_message_append(m, "a(sa(sv))", 0);
if (r < 0) {
fprintf(stderr, "Failed appending aux boilerplate\n");
goto finish;
}
r = sd_bus_call(NULL, m, 0, &error, &reply);
if (r < 0) {
fprintf(stderr, "Failed sending message: %s\n", error.message);
goto finish;
}
/* Parse the response message */
r = sd_bus_message_read(reply, "o", &path);
if (r < 0) {
fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r));
goto finish;
}
printf("Queued service job as %s.\n", path);
printf("I am %d\n", pid);
finish:
sd_bus_error_free(&error);
sd_bus_message_unref(reply);
sd_bus_message_unref(m);
sd_bus_unref(bus);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
print_current_cgroup();
enter_new_cgroup("lxc/test");
print_current_cgroup();
}
a: a.c
gcc a.c -o a `pkg-config --cflags --libs libsystemd`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment