Skip to content
Browse files

move hierarchy around

  • Loading branch information...
1 parent d6a04b7 commit e9daa545a674440d1150e14a8045fa67b23830f8 @vodik committed
View
5 Makefile
@@ -6,9 +6,10 @@ CFLAGS := -std=c99 \
LDLIBS = -ldbus-1
-all: clique
+all: example
-clique: clique.o dbus-lib.o dbus-util.o dbus-systemd.o
+example: example.o systemd-scope.o systemd-unit.o \
+ dbus/dbus-shim.o dbus/dbus-util.o
clean:
${RM} clique *.o
View
18 dbus-systemd.h
@@ -1,18 +0,0 @@
-#ifndef DBUS_SYSTEMD
-#define DBUS_SYSTEMD
-
-#include "dbus-lib.h"
-
-int scope_init(dbus_message **m, const char *name, const char *slice,
- const char *desc, int32_t pid);
-
-int scope_memory_limit(dbus_message *m, int32_t limit);
-
-int scope_commit(dbus_bus *bus, dbus_message *m, char **ret);
-
-int get_unit_by_pid(dbus_bus *bus, uint32_t pid, char **ret);
-int get_unit(dbus_bus *bus, const char *name, char **ret);
-
-void unit_kill(dbus_bus *bus, const char *path, int32_t signal);
-
-#endif
View
2 dbus-lib.c → dbus/dbus-shim.c
@@ -1,4 +1,4 @@
-#include "dbus-lib.h"
+#include "dbus-shim.h"
#include <stdlib.h>
#include <stdio.h>
View
0 dbus-lib.h → dbus/dbus-shim.h
File renamed without changes.
View
3 dbus-util.c → dbus/dbus-util.c
@@ -1,6 +1,5 @@
#include "dbus-util.h"
-
-#include "dbus-lib.h"
+#include "dbus-shim.h"
int query_property(dbus_bus *bus, const char *path, const char *interface,
const char *property, const char *types, ...)
View
2 dbus-util.h → dbus/dbus-util.h
@@ -1,7 +1,7 @@
#ifndef DBUS_UTIL_H
#define DBUS_UTIL_H
-#include "dbus-lib.h"
+#include "dbus-shim.h"
int query_property(dbus_bus *bus, const char *path, const char *interface,
const char *property, const char *types, ...);
View
13 clique.c → example.c
@@ -6,9 +6,10 @@
#include <signal.h>
#include <unistd.h>
-#include "dbus-lib.h"
-#include "dbus-util.h"
-#include "dbus-systemd.h"
+#include "dbus/dbus-shim.h"
+#include "dbus/dbus-util.h"
+#include "systemd-scope.h"
+#include "systemd-unit.h"
static void busy_loop()
{
@@ -48,8 +49,7 @@ int main(void)
printf("PATH: %s\n", path);
/* check state */
- query_property(bus, path, "org.freedesktop.systemd1.Unit",
- "SubState", "s", &state);
+ get_unit_state(bus, path, &state);
printf("State: %s\n", state);
/* kill */
@@ -57,7 +57,6 @@ int main(void)
unit_kill(bus, path, SIGTERM);
/* check state */
- query_property(bus, path, "org.freedesktop.systemd1.Unit",
- "SubState", "s", &state);
+ get_unit_state(bus, path, &state);
printf("State: %s\n", state);
}
View
73 systemd-scope.c
@@ -0,0 +1,73 @@
+#include "systemd-scope.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "dbus/dbus-shim.h"
+
+static inline int dbus_try_read_object(dbus_bus *bus, dbus_message *reply, char **ret)
+{
+ int type = dbus_message_type(reply);
+ const char *buf;
+ int rc = 0;
+
+ if (type == 'o') {
+ rc = dbus_message_read(reply, "o", &buf);
+ *ret = strdup(buf);
+ } else if (type == 's') {
+ rc = -1;
+ dbus_message_read(reply, "s", &buf);
+ free(bus->error);
+ bus->error = strdup(buf);
+ }
+
+ return rc;
+}
+
+int scope_init(dbus_message **m, const char *name, const char *slice,
+ const char *desc, int32_t pid)
+{
+ /* char *path; */
+ dbus_new_method_call("org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartTransientUnit", m);
+
+ dbus_message_append(*m, "ss", name, "fail");
+ dbus_open_container(*m, 'a', "(sv)");
+
+ if (desc)
+ dbus_message_append(*m, "(sv)", "Description", "s", desc);
+ if (slice)
+ dbus_message_append(*m, "(sv)", "Slice", "s", slice);
+
+ dbus_message_append(*m, "(sv)", "PIDs", "au", 1, pid ? pid : getpid());
+
+ return 0;
+}
+
+int scope_memory_limit(dbus_message *m, int32_t limit)
+{
+ dbus_message_append(m, "(sv)", "MemoryLimit", "t", limit);
+ return 0;
+}
+
+int scope_commit(dbus_bus *bus, dbus_message *m, char **ret)
+{
+ int rc = 0;
+
+ dbus_close_container(m);
+
+ dbus_message *reply;
+ dbus_send_with_reply_and_block(bus, m, ret ? &reply : NULL);
+ dbus_message_free(m);
+
+ if (ret) {
+ rc = dbus_try_read_object(bus, reply, ret);
+ dbus_message_free(reply);
+ }
+
+ return rc;
+}
View
13 systemd-scope.h
@@ -0,0 +1,13 @@
+#ifndef SYSTEMD_SCOPE
+#define SYSTEMD_SCOPE
+
+#include "dbus/dbus-shim.h"
+
+int scope_init(dbus_message **m, const char *name, const char *slice,
+ const char *desc, int32_t pid);
+
+int scope_memory_limit(dbus_message *m, int32_t limit);
+
+int scope_commit(dbus_bus *bus, dbus_message *m, char **ret);
+
+#endif
View
53 dbus-systemd.c → systemd-unit.c
@@ -1,11 +1,12 @@
-#include "dbus-systemd.h"
+#include "systemd-unit.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
-#include "dbus-lib.h"
+#include "dbus/dbus-shim.h"
+#include "dbus/dbus-util.h"
static inline int dbus_try_read_object(dbus_bus *bus, dbus_message *reply, char **ret)
{
@@ -88,50 +89,8 @@ void unit_kill(dbus_bus *bus, const char *path, int32_t signal)
dbus_message_free(m);
}
-////////////////////////
-
-int scope_init(dbus_message **m, const char *name, const char *slice,
- const char *desc, int32_t pid)
-{
- /* char *path; */
- dbus_new_method_call("org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "StartTransientUnit", m);
-
- dbus_message_append(*m, "ss", name, "fail");
- dbus_open_container(*m, 'a', "(sv)");
-
- if (desc)
- dbus_message_append(*m, "(sv)", "Description", "s", desc);
- if (slice)
- dbus_message_append(*m, "(sv)", "Slice", "s", slice);
-
- dbus_message_append(*m, "(sv)", "PIDs", "au", 1, pid ? pid : getpid());
-
- return 0;
-}
-
-int scope_memory_limit(dbus_message *m, int32_t limit)
-{
- dbus_message_append(m, "(sv)", "MemoryLimit", "t", limit);
- return 0;
-}
-
-int scope_commit(dbus_bus *bus, dbus_message *m, char **ret)
+int get_unit_state(dbus_bus *bus, const char *path, char **ret)
{
- int rc = 0;
-
- dbus_close_container(m);
-
- dbus_message *reply;
- dbus_send_with_reply_and_block(bus, m, ret ? &reply : NULL);
- dbus_message_free(m);
-
- if (ret) {
- rc = dbus_try_read_object(bus, reply, ret);
- dbus_message_free(reply);
- }
-
- return rc;
+ return query_property(bus, path, "org.freedesktop.systemd1.Unit",
+ "SubState", "s", ret);
}
View
11 systemd-unit.h
@@ -0,0 +1,11 @@
+#ifndef SYSTEMD_UNIT
+#define SYSTEMD_UNIT
+
+#include "dbus/dbus-shim.h"
+
+int get_unit_by_pid(dbus_bus *bus, uint32_t pid, char **ret);
+int get_unit(dbus_bus *bus, const char *name, char **ret);
+void unit_kill(dbus_bus *bus, const char *path, int32_t signal);
+int get_unit_state(dbus_bus *bus, const char *path, char **ret);
+
+#endif

0 comments on commit e9daa54

Please sign in to comment.
Something went wrong with that request. Please try again.