2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/param.h>
24 #include <glib/gprintf.h>
25 #include <glib-unix.h>
26 #include <polkit/polkit.h>
28 #include "logind-gen.h"
31 #include "../../polkit-auth.h"
33 GPtrArray
*logind_freeable
;
34 Login1Manager
*logind_interf
;
36 GMainLoop
*logind_loop
;
39 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
41 /* --- begin method/property/dbus signal code --- */
44 on_handle_set_hostname(Login1Manager *hn1_passed_interf,
45 GDBusMethodInvocation *invoc,
54 gchar *hostname_buf, *ret;
55 size_t hostname_divider;
57 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
58 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
59 g_ptr_array_add(logind_freeable, hostname_buf);
60 g_ptr_array_add(logind_freeable, ret);
62 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
65 hostname_divider = strcspn(hostname_buf, ".");
67 return strncpy(ret, hostname_buf, hostname_divider);
70 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
72 static void logind_on_bus_acquired(GDBusConnection
*conn
,
76 g_printf("got bus/name, exporting %s's interface...\n", name
);
78 logind_interf
= login1_manager_skeleton_new();
80 /* attach function pointers to generated struct's method handlers
81 g_signal_connect(logind_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */
83 /* set our properties before export
84 login1_manager_set_hostname(logind_interf, our_get_hostname()); */
86 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(logind_interf
),
88 "/org/freedesktop/login1_manager",
91 g_printf("failed to export %s's interface!\n", name
);
96 dbus_interface_exported
= TRUE
;
97 g_printf("exported %s's interface on the system bus...\n", name
);
101 static void logind_on_name_acquired(GDBusConnection
*conn
,
103 gpointer user_data
) {
105 g_printf("success!\n");
108 static void logind_on_name_lost(GDBusConnection
*conn
,
110 gpointer user_data
) {
114 g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name
);
118 g_print("lost name %s, exiting...\n", name
);
123 /* --- end bus/name handlers, begin misc unix functions --- */
125 /* safe call to clean and then exit
126 * this stops our GMainLoop sfaely before letting main() return */
127 void logind_mem_clean() {
129 g_printf("exiting...\n");
131 if(dbus_interface_exported
)
132 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(logind_interf
));
134 if(g_main_loop_is_running(logind_loop
))
135 g_main_loop_quit(logind_loop
);
139 /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */
140 gboolean
unix_sig_terminate_handler(gpointer data
) {
142 g_printf("caught SIGINT/HUP/TERM, exiting\n");
145 return G_SOURCE_REMOVE
;
148 void set_signal_handlers() {
150 /* we don't care about its descriptor, we never need to unregister these */
151 g_unix_signal_add(SIGINT
, unix_sig_terminate_handler
, NULL
);
152 g_unix_signal_add(SIGHUP
, unix_sig_terminate_handler
, NULL
);
153 g_unix_signal_add(SIGTERM
, unix_sig_terminate_handler
, NULL
);
158 set_signal_handlers();
160 logind_loop
= g_main_loop_new(NULL
, TRUE
);
161 logind_freeable
= g_ptr_array_new();
163 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
164 "org.freedesktop.login1_manager",
165 G_BUS_NAME_OWNER_FLAGS_NONE
,
166 logind_on_bus_acquired
,
167 logind_on_name_acquired
,
172 g_main_loop_run(logind_loop
);
173 /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */
174 g_main_loop_unref(logind_loop
);
176 /* guaranteed unownable */
177 g_bus_unown_name(bus_descriptor
);
179 /* at this point no operations can occur with our data, it is safe to free it + its container */
180 g_ptr_array_free(logind_freeable
, TRUE
);