usb_moded 0.86.0+mer58
usb_moded-user.c
Go to the documentation of this file.
1
24
25#include "usb_moded-user.h"
26#include "usb_moded-control.h"
27#include "usb_moded-log.h"
28
29#include <systemd/sd-login.h>
30
31/* ========================================================================= *
32 * Prototypes
33 * ========================================================================= */
34
35/* ------------------------------------------------------------------------- *
36 * USER
37 * ------------------------------------------------------------------------- */
38
39static void user_update_current_user(void);
40static void user_set_current_user (uid_t uid);
41uid_t user_get_current_user (void);
42
43/* ------------------------------------------------------------------------- *
44 * USER_WATCH
45 * ------------------------------------------------------------------------- */
46
47static gboolean user_watch_monitor_event_cb(GIOChannel *iochannel, GIOCondition cond, gpointer data);
48static bool user_watch_connect (void);
49static void user_watch_disconnect (void);
50bool user_watch_init (void);
51void user_watch_stop (void);
52
53/* ========================================================================= *
54 * Data
55 * ========================================================================= */
56
57static sd_login_monitor *user_watch_monitor = NULL;
58static guint user_change_watch_id = 0;
59static uid_t user_current_uid = UID_UNKNOWN;
60
61/* ========================================================================= *
62 * Functions
63 * ========================================================================= */
64
71static void user_update_current_user(void)
72{
73#ifdef SAILFISH_ACCESS_CONTROL
74 uid_t uid = UID_UNKNOWN;
75 sd_seat_get_active("seat0", 0, &uid);
76 user_set_current_user(uid);
77#else
78 user_set_current_user(0);
79#endif
80}
81
82static void user_set_current_user(uid_t uid)
83{
84 // User changed
85 if ( user_current_uid != uid ) {
86 log_debug("user_current_uid: %d -> %d",
87 (int)user_current_uid, (int)uid);
88 user_current_uid = uid;
90 }
91}
92
105{
106 return user_current_uid;
107}
108
111static gboolean user_watch_monitor_event_cb(GIOChannel *iochannel G_GNUC_UNUSED, GIOCondition cond,
112 gpointer data G_GNUC_UNUSED)
113{
114 LOG_REGISTER_CONTEXT;
115 bool success = true;
116
117 if( cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL) ) {
118 log_crit("user watch hangup/error");
119 success = false;
120 goto EXIT;
121 }
122 user_update_current_user();
123
124 sd_login_monitor_flush(user_watch_monitor);
125
126EXIT:
127 if ( !success ) {
128 user_change_watch_id = 0;
129 user_watch_disconnect();
130 return G_SOURCE_REMOVE;
131 }
132 return G_SOURCE_CONTINUE;
133}
134
135static bool user_watch_connect(void)
136{
137 LOG_REGISTER_CONTEXT;
138
139 bool success = false;
140 GIOChannel *iochan = NULL;
141
142 if ( sd_login_monitor_new("uid", &user_watch_monitor) < 0 ) {
143 log_err("Failed to create login monitor\n");
144 goto EXIT;
145 }
146
147 user_update_current_user();
148
149 iochan = g_io_channel_unix_new(sd_login_monitor_get_fd(user_watch_monitor));
150 if ( !iochan ) {
151 log_err("Failed to setup I/O channel for sd_login_monitor\n");
152 goto EXIT;
153 }
154
155 user_change_watch_id = g_io_add_watch(iochan,
156 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
157 user_watch_monitor_event_cb,
158 NULL);
159 if ( user_change_watch_id )
160 success = true;
161
162EXIT:
163 if ( iochan )
164 g_io_channel_unref(iochan);
165 if ( !success )
166 user_watch_disconnect();
167 return success;
168}
169
170static void user_watch_disconnect(void)
171{
172 LOG_REGISTER_CONTEXT;
173
174 if ( user_change_watch_id ) {
175 g_source_remove(user_change_watch_id);
176 user_change_watch_id = 0;
177 }
178 if ( user_watch_monitor ) {
179 sd_login_monitor_unref(user_watch_monitor);
180 user_watch_monitor = NULL;
181 }
182}
183
184bool user_watch_init(void)
185{
186 LOG_REGISTER_CONTEXT;
187
188 return user_watch_connect();
189}
190
191void user_watch_stop(void)
192{
193 LOG_REGISTER_CONTEXT;
194
195 user_watch_disconnect();
196}
void control_user_changed(void)
uid_t user_get_current_user(void)