Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Diviector/391d441071f7c1d83106835f3a32da9f to your computer and use it in GitHub Desktop.
Save Diviector/391d441071f7c1d83106835f3a32da9f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>
int main() {
DBusError error;
DBusConnection *bus;
DBusMessage *message, *reply;
dbus_error_init(&error);
// Connect to the system bus
bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
if (dbus_error_is_set(&error)) {
fprintf(stderr, "Error connecting to the system bus: %s\n", error.message);
dbus_error_free(&error);
exit(EXIT_FAILURE);
}
// Create a message to stop the service
message = dbus_message_new_method_call("org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StopUnit");
dbus_message_append_args(message,
DBUS_TYPE_STRING, "service-name.service",
DBUS_TYPE_STRING, "replace",
&error);
// Send the message and wait for a reply
reply = dbus_connection_send_with_reply_and_block(bus, message, -1, &error);
if (dbus_error_is_set(&error)) {
fprintf(stderr, "Error sending message: %s\n", error.message);
dbus_error_free(&error);
exit(EXIT_FAILURE);
}
dbus_message_unref(message);
dbus_message_unref(reply);
dbus_connection_unref(bus);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment