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