aboutsummaryrefslogblamecommitdiff
path: root/src/npassd/npassd.c
blob: a2f5173082deef00dff2bfecc0522f8c8f02acea (plain) (tree)






























































                                                                                
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>

#include "npassd/service.h"
#include "util.h"

#define DBUS_WELL_KNOWN_NAME "org.freedesktop.secrets"

int main(void)
{
	int ret;
	sd_bus *bus;
	struct service service;

	ret = sd_bus_open_user(&bus);
	if (ret < 0) {
		print_err("Failed to connect to bus: %s", strerror(-ret));
		return EXIT_FAILURE;
	}

	ret = sd_bus_request_name(bus, DBUS_WELL_KNOWN_NAME, 0);
	if (ret < 0) {
		print_err("Failed to acquire service name: %s", strerror(-ret));
		if (ret == -EEXIST)
			print_err(
				"%s",
				"Is a secret-service daemon already running?");

		goto out_free_bus;
	}

	ret = service_init(bus, &service);
	if (ret < 0)
		goto out_free_bus;

	for (;;) {
		ret = sd_bus_process(bus, NULL);
		if (ret < 0) {
			fprintf(stderr, "Failed to process bus: %s\n",
				strerror(-ret));
			goto out_free_service;
		}
		if (ret > 0)
			continue;

		ret = sd_bus_wait(bus, UINT64_MAX);
		if (ret < 0) {
			fprintf(stderr, "Failed to wait on bus: %s\n",
				strerror(-ret));
			goto out_free_service;
		}
	}

out_free_service:
	service_free(&service);
out_free_bus:
	sd_bus_unref(bus);

	return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}