3b9cab68686c73dcc592e1e45043e5c581a62c41
[systembsd.git] / src / interfaces / logind / logind.c
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 */
16
17 #include <unistd.h>
18 #include <limits.h>
19 #include <signal.h>
20
21 #include <sys/param.h>
22 #include <string.h>
23
24 #include <glib/gprintf.h>
25 #include <glib-unix.h>
26
27 #include "logind-gen.h"
28 #include "logind.h"
29
30 GPtrArray *logind_freeable;
31 Login1Manager *logind_interf;
32
33 GMainLoop *logind_loop;
34
35 guint bus_descriptor;
36 gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */
37
38 /* --- begin method/property/dbus signal code --- */
39
40 /*static gboolean
41 on_handle_set_hostname(Login1Manager *hn1_passed_interf,
42 GDBusMethodInvocation *invoc,
43 const gchar *greet,
44 gpointer data) {
45 return FALSE;
46 }*/
47
48 /*const gchar *
49 our_get_hostname() {
50
51 gchar *hostname_buf, *ret;
52 size_t hostname_divider;
53
54 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
55 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
56 g_ptr_array_add(logind_freeable, hostname_buf);
57 g_ptr_array_add(logind_freeable, ret);
58
59 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
60 return "";
61
62 hostname_divider = strcspn(hostname_buf, ".");
63
64 return strncpy(ret, hostname_buf, hostname_divider);
65 }*/
66
67 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
68
69 static void logind_on_bus_acquired(GDBusConnection *conn,
70 const gchar *name,
71 gpointer user_data) {
72
73 g_print("got bus, name: %s\n", name);
74
75 }
76
77 static void logind_on_name_acquired(GDBusConnection *conn,
78 const gchar *name,
79 gpointer user_data) {
80
81 g_print("got '%s' on system bus\n", name);
82
83 logind_interf = login1_manager_skeleton_new();
84
85 /* attach function pointers to generated struct's method handlers
86 g_signal_connect(logind_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */
87
88 /* set our properties before export
89 login1_manager_set_hostname(logind_interf, our_get_hostname()); */
90
91 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(logind_interf),
92 conn,
93 "/org/freedesktop/login1_manager",
94 NULL)) {
95
96 g_printf("Failed to export Login1Manager's interface!");
97 }
98
99 }
100
101 static void logind_on_name_lost(GDBusConnection *conn,
102 const gchar *name,
103 gpointer user_data) {
104
105 g_print("lost name %s, exiting...", name);
106
107 logind_mem_clean();
108 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(logind_interf));
109
110 }
111
112 /* --- end bus/name handlers, begin misc unix functions --- */
113
114 /* free()'s */
115 void logind_mem_clean() {
116
117 g_ptr_array_foreach(logind_freeable, (GFunc) g_free, NULL);
118 g_ptr_array_free(logind_freeable, TRUE);
119 }
120
121 int main() {
122
123 logind_loop = g_main_loop_new(NULL, TRUE);
124 logind_freeable = g_ptr_array_new();
125
126 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
127 "org.freedesktop.login1_manager",
128 G_BUS_NAME_OWNER_FLAGS_NONE,
129 logind_on_bus_acquired,
130 logind_on_name_acquired,
131 logind_on_name_lost,
132 NULL,
133 NULL);
134
135 g_main_loop_run(logind_loop);
136 g_main_loop_unref(logind_loop);
137
138 g_bus_unown_name(bus_descriptor);
139
140 logind_mem_clean();
141
142 return 0;
143 }