uploaded spice-vdagent
[platform/adaptation/emulator/spice-vdagent.git] / src / systemd-login.c
1 /*  systemd-login.c vdagentd libsystemd-login integration code
2
3     Copyright 2012 Red Hat, Inc.
4
5     Red Hat Authors:
6     Hans de Goede <hdegoede@redhat.com>
7
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or   
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of 
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "session-info.h"
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <syslog.h>
27 #include <systemd/sd-login.h>
28
29 struct session_info {
30     int verbose;
31     sd_login_monitor *mon;
32     char *session;
33 };
34
35 struct session_info *session_info_create(int verbose)
36 {
37     struct session_info *si;
38     int r;
39
40     si = calloc(1, sizeof(*si));
41     if (!si)
42         return NULL;
43
44     si->verbose = verbose;
45
46     r = sd_login_monitor_new("session", &si->mon);
47     if (r < 0) {
48         syslog(LOG_ERR, "Error creating login monitor: %s", strerror(-r));
49         free(si);
50         return NULL;
51     }
52
53     return si;
54 }
55
56 void session_info_destroy(struct session_info *si)
57 {
58     if (!si)
59         return;
60
61     sd_login_monitor_unref(si->mon);
62     free(si->session);
63     free(si);
64 }
65
66 int session_info_get_fd(struct session_info *si)
67 {
68     return sd_login_monitor_get_fd(si->mon);
69 }
70
71 const char *session_info_get_active_session(struct session_info *si)
72 {
73     int r;
74     char *old_session = si->session;
75
76     si->session = NULL;
77     r = sd_seat_get_active("seat0", &si->session, NULL);
78     /* ENOENT happens when a seat is switching from one session to another */
79     if (r < 0 && r != -ENOENT)
80         syslog(LOG_ERR, "Error getting active session: %s",
81                 strerror(-r));
82
83     if (si->verbose && si->session &&
84             (!old_session || strcmp(old_session, si->session)))
85         syslog(LOG_INFO, "Active session: %s", si->session);
86
87     sd_login_monitor_flush(si->mon);
88     free(old_session);
89
90     return si->session;
91 }
92
93 char *session_info_session_for_pid(struct session_info *si, uint32_t pid)
94 {
95     int r;
96     char *session = NULL;
97
98     r = sd_pid_get_session(pid, &session);
99     if (r < 0)
100         syslog(LOG_ERR, "Error getting session for pid %u: %s",
101                 pid, strerror(-r));
102     else if (si->verbose)
103         syslog(LOG_INFO, "Session for pid %u: %s", pid, session);
104
105     return session;
106 }