summaryrefslogtreecommitdiffstats
path: root/src/systemd-login.c
blob: 73db37fa79de1e624e91f8a5318dc4fda8524dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*  systemd-login.c vdagentd libsystemd-login integration code

    Copyright 2012 Red Hat, Inc.

    Red Hat Authors:
    Hans de Goede <hdegoede@redhat.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or   
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "session-info.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <systemd/sd-login.h>

struct session_info {
    int verbose;
    sd_login_monitor *mon;
    char *session;
};

struct session_info *session_info_create(int verbose)
{
    struct session_info *si;
    int r;

    si = calloc(1, sizeof(*si));
    if (!si)
        return NULL;

    si->verbose = verbose;

    r = sd_login_monitor_new("session", &si->mon);
    if (r < 0) {
        syslog(LOG_ERR, "Error creating login monitor: %s", strerror(-r));
        free(si);
        return NULL;
    }

    return si;
}

void session_info_destroy(struct session_info *si)
{
    if (!si)
        return;

    sd_login_monitor_unref(si->mon);
    free(si->session);
    free(si);
}

int session_info_get_fd(struct session_info *si)
{
    return sd_login_monitor_get_fd(si->mon);
}

const char *session_info_get_active_session(struct session_info *si)
{
    int r;
    char *old_session = si->session;

    si->session = NULL;
    r = sd_seat_get_active("seat0", &si->session, NULL);
    /* ENOENT happens when a seat is switching from one session to another */
    if (r < 0 && r != -ENOENT)
        syslog(LOG_ERR, "Error getting active session: %s",
                strerror(-r));

    if (si->verbose && si->session &&
            (!old_session || strcmp(old_session, si->session)))
        syslog(LOG_INFO, "Active session: %s", si->session);

    sd_login_monitor_flush(si->mon);
    free(old_session);

    return si->session;
}

char *session_info_session_for_pid(struct session_info *si, uint32_t pid)
{
    int r;
    char *session = NULL;

    r = sd_pid_get_session(pid, &session);
    if (r < 0)
        syslog(LOG_ERR, "Error getting session for pid %u: %s",
                pid, strerror(-r));
    else if (si->verbose)
        syslog(LOG_INFO, "Session for pid %u: %s", pid, session);

    return session;
}