baa40e28 |
1 | /* |
2 | * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc> |
3 | * |
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. |
7 | * |
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. |
15 | */ |
2eae47af |
16 | |
17 | #include <unistd.h> |
18 | #include <limits.h> |
f50e1f3b |
19 | #include <signal.h> |
2eae47af |
20 | |
21 | #include <sys/param.h> |
22 | #include <string.h> |
23 | |
24 | #include <glib/gprintf.h> |
ba9914b6 |
25 | #include <glib-unix.h> |
eb78abe9 |
26 | #include <polkit/polkit.h> |
2eae47af |
27 | |
3c3794ac |
28 | #include "logind-gen.h" |
ce19cb8a |
29 | #include "logind.h" |
baa40e28 |
30 | |
2eae47af |
31 | GPtrArray *logind_freeable; |
32 | Login1Manager *logind_interf; |
33 | |
f50e1f3b |
34 | GMainLoop *logind_loop; |
35 | |
36 | guint bus_descriptor; |
37 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ |
38 | |
ff036f75 |
39 | /* --- begin method/property/dbus signal code --- */ |
2eae47af |
40 | |
41 | /*static gboolean |
42 | on_handle_set_hostname(Login1Manager *hn1_passed_interf, |
43 | GDBusMethodInvocation *invoc, |
44 | const gchar *greet, |
45 | gpointer data) { |
46 | return FALSE; |
47 | }*/ |
48 | |
49 | /*const gchar * |
50 | our_get_hostname() { |
51 | |
99ef55dd |
52 | gchar *hostname_buf, *ret; |
53 | size_t hostname_divider; |
2eae47af |
54 | |
99ef55dd |
55 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
56 | ret = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
57 | g_ptr_array_add(logind_freeable, hostname_buf); |
58 | g_ptr_array_add(logind_freeable, ret); |
2eae47af |
59 | |
99ef55dd |
60 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) |
61 | return ""; |
2eae47af |
62 | |
99ef55dd |
63 | hostname_divider = strcspn(hostname_buf, "."); |
2eae47af |
64 | |
99ef55dd |
65 | return strncpy(ret, hostname_buf, hostname_divider); |
2eae47af |
66 | }*/ |
67 | |
ff036f75 |
68 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
2eae47af |
69 | |
70 | static void logind_on_bus_acquired(GDBusConnection *conn, |
71 | const gchar *name, |
72 | gpointer user_data) { |
73 | |
a0d8931a |
74 | g_printf("got bus/name, exporting %s's interface...\n", name); |
2eae47af |
75 | |
76 | logind_interf = login1_manager_skeleton_new(); |
77 | |
78 | /* attach function pointers to generated struct's method handlers |
79 | g_signal_connect(logind_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */ |
80 | |
81 | /* set our properties before export |
82 | login1_manager_set_hostname(logind_interf, our_get_hostname()); */ |
83 | |
84 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(logind_interf), |
85 | conn, |
86 | "/org/freedesktop/login1_manager", |
87 | NULL)) { |
88 | |
ca5a7f9f |
89 | g_printf("failed to export %s's interface!\n", name); |
90 | logind_mem_clean(); |
91 | |
92 | } else { |
93 | |
94 | dbus_interface_exported = TRUE; |
95 | g_printf("exported %s's interface on the system bus...\n", name); |
2eae47af |
96 | } |
ca5a7f9f |
97 | } |
2eae47af |
98 | |
ca5a7f9f |
99 | static void logind_on_name_acquired(GDBusConnection *conn, |
99ef55dd |
100 | const gchar *name, |
ca5a7f9f |
101 | gpointer user_data) { |
102 | |
103 | g_printf("success!\n"); |
2eae47af |
104 | } |
105 | |
2eae47af |
106 | static void logind_on_name_lost(GDBusConnection *conn, |
107 | const gchar *name, |
108 | gpointer user_data) { |
109 | |
ca5a7f9f |
110 | if(!conn) { |
2eae47af |
111 | |
ca5a7f9f |
112 | 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); |
2eae47af |
113 | |
ca5a7f9f |
114 | } |
115 | |
116 | g_print("lost name %s, exiting...\n", name); |
117 | |
118 | logind_mem_clean(); |
2eae47af |
119 | } |
120 | |
eb20fe6d |
121 | /* --- end bus/name handlers, begin misc unix functions --- */ |
122 | |
ca5a7f9f |
123 | /* safe call to clean and then exit |
124 | * this stops our GMainLoop sfaely before letting main() return */ |
eb20fe6d |
125 | void logind_mem_clean() { |
126 | |
5a2ebcdb |
127 | g_printf("exiting...\n"); |
128 | |
129 | if(dbus_interface_exported) |
130 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(logind_interf)); |
131 | |
132 | if(g_main_loop_is_running(logind_loop)) |
133 | g_main_loop_quit(logind_loop); |
134 | |
eb20fe6d |
135 | } |
136 | |
a6e4b32c |
137 | /* wrapper for glib's unix signal handling; called only once if terminating signal is raised against us */ |
138 | gboolean unix_sig_terminate_handler(gpointer data) { |
139 | |
140 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
141 | |
142 | logind_mem_clean(); |
143 | return G_SOURCE_REMOVE; |
144 | } |
145 | |
34d88d96 |
146 | void set_signal_handlers() { |
147 | |
148 | /* we don't care about its descriptor, we never need to unregister these */ |
149 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); |
150 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); |
151 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); |
152 | } |
153 | |
3c3794ac |
154 | int main() { |
2eae47af |
155 | |
7a088364 |
156 | set_signal_handlers(); |
157 | |
99ef55dd |
158 | logind_loop = g_main_loop_new(NULL, TRUE); |
159 | logind_freeable = g_ptr_array_new(); |
2eae47af |
160 | |
99ef55dd |
161 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
2eae47af |
162 | "org.freedesktop.login1_manager", |
163 | G_BUS_NAME_OWNER_FLAGS_NONE, |
164 | logind_on_bus_acquired, |
165 | logind_on_name_acquired, |
166 | logind_on_name_lost, |
167 | NULL, |
168 | NULL); |
169 | |
99ef55dd |
170 | g_main_loop_run(logind_loop); |
7a088364 |
171 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ |
99ef55dd |
172 | g_main_loop_unref(logind_loop); |
2eae47af |
173 | |
7a088364 |
174 | /* guaranteed unownable */ |
99ef55dd |
175 | g_bus_unown_name(bus_descriptor); |
2eae47af |
176 | |
7a088364 |
177 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
178 | g_ptr_array_free(logind_freeable, TRUE); |
179 | |
2eae47af |
180 | |
99ef55dd |
181 | return 0; |
3c3794ac |
182 | } |