Created
November 1, 2019 13:15
-
-
Save lighth7015/72aa0aaf7f573cb42a41011233657f5e to your computer and use it in GitHub Desktop.
Query systemd's user bus to check for a service's existence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <systemd/sd-bus.h> | |
static sd_bus_error error = SD_BUS_ERROR_NULL; | |
static sd_bus_message *response; | |
static sd_bus *bus; | |
static int result; | |
static const char *path; | |
const char* service_name = "org.freedesktop.systemd1", | |
* service_path = "/org/freedesktop/systemd1"; | |
const char* interface = "org.freedesktop.systemd1.Manager", | |
* operation_id = "GetUnit"; | |
const char* signature = "s", | |
* serviceId = "connector.service"; | |
// Just because I prefer to look at cleaner API names. | |
#define RpcQueryMethod sd_bus_call_method | |
#define RpcBusConnect sd_bus_open_user | |
#define GetErrMessage strerror | |
int main(int argc, char *argv[]) { | |
if (( result = -RpcBusConnect( &bus ))) { | |
fprintf(stderr, "Failed to connect to system bus: %s\n", GetErrMessage(result)); | |
} | |
else { | |
if (( result = RpcQueryMethod(bus, service_name, service_path, interface, | |
operation_id, &error, &response, signature, | |
serviceId ))) | |
{ | |
printf( "Service Manager: %s\n", error.message); | |
} | |
else { | |
/* Parse the response message */ | |
if (( result = sd_bus_message_read( response, "o", &path ))) { | |
printf("Failed to parse response message: %s\n", strerror(-result)); | |
} | |
else { | |
puts("Got unit?"); | |
} | |
} | |
} | |
sd_bus_error_free(&error); | |
sd_bus_message_unref(response); | |
sd_bus_unref(bus); | |
return result? EXIT_SUCCESS: EXIT_FAILURE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment