1 /* systemd-login.c vdagentd libsystemd-login integration code
3 Copyright 2012 Red Hat, Inc.
6 Hans de Goede <hdegoede@redhat.com>
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.
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.
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/>.
22 #include "session-info.h"
27 #include <systemd/sd-login.h>
31 sd_login_monitor *mon;
35 struct session_info *session_info_create(int verbose)
37 struct session_info *si;
40 si = calloc(1, sizeof(*si));
44 si->verbose = verbose;
46 r = sd_login_monitor_new("session", &si->mon);
48 syslog(LOG_ERR, "Error creating login monitor: %s", strerror(-r));
56 void session_info_destroy(struct session_info *si)
61 sd_login_monitor_unref(si->mon);
66 int session_info_get_fd(struct session_info *si)
68 return sd_login_monitor_get_fd(si->mon);
71 const char *session_info_get_active_session(struct session_info *si)
74 char *old_session = si->session;
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",
83 if (si->verbose && si->session &&
84 (!old_session || strcmp(old_session, si->session)))
85 syslog(LOG_INFO, "Active session: %s", si->session);
87 sd_login_monitor_flush(si->mon);
93 char *session_info_session_for_pid(struct session_info *si, uint32_t pid)
98 r = sd_pid_get_session(pid, &session);
100 syslog(LOG_ERR, "Error getting session for pid %u: %s",
102 else if (si->verbose)
103 syslog(LOG_INFO, "Session for pid %u: %s", pid, session);