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.
20 #include <sys/param.h>
23 #include <glib/gprintf.h>
25 #include "logind-gen.h"
27 GPtrArray
*logind_freeable
;
28 Login1Manager
*logind_interf
;
30 /* --- begin method/property/signal code --- */
33 on_handle_set_hostname(Login1Manager *hn1_passed_interf,
34 GDBusMethodInvocation *invoc,
43 gchar *hostname_buf, *ret;
44 size_t hostname_divider;
46 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
47 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
48 g_ptr_array_add(logind_freeable, hostname_buf);
49 g_ptr_array_add(logind_freeable, ret);
51 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
54 hostname_divider = strcspn(hostname_buf, ".");
56 return strncpy(ret, hostname_buf, hostname_divider);
59 /* --- end method/property/signal code, begin bus/name handlers --- */
61 static void logind_on_bus_acquired(GDBusConnection
*conn
,
65 g_print("got bus, name: %s\n", name
);
69 static void logind_on_name_acquired(GDBusConnection
*conn
,
73 g_print("got '%s' on system bus\n", name
);
75 logind_interf
= login1_manager_skeleton_new();
77 /* attach function pointers to generated struct's method handlers
78 g_signal_connect(logind_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */
80 /* set our properties before export
81 login1_manager_set_hostname(logind_interf, our_get_hostname()); */
83 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(logind_interf
),
85 "/org/freedesktop/login1_manager",
88 g_printf("Failed to export Login1Manager's interface!");
93 /* --- end bus/name handlers, begin misc functions --- */
96 void logind_mem_clean() {
98 g_ptr_array_foreach(logind_freeable
, (GFunc
) g_free
, NULL
);
99 g_ptr_array_free(logind_freeable
, TRUE
);
102 static void logind_on_name_lost(GDBusConnection
*conn
,
104 gpointer user_data
) {
106 g_print("lost name %s, exiting...", name
);
109 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(logind_interf
));
115 guint bus_descriptor
;
116 GMainLoop
*logind_loop
;
118 logind_loop
= g_main_loop_new(NULL
, TRUE
);
119 logind_freeable
= g_ptr_array_new();
121 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
122 "org.freedesktop.login1_manager",
123 G_BUS_NAME_OWNER_FLAGS_NONE
,
124 logind_on_bus_acquired
,
125 logind_on_name_acquired
,
130 g_main_loop_run(logind_loop
);
131 g_main_loop_unref(logind_loop
);
133 g_bus_unown_name(bus_descriptor
);