source: ldm/trunk/fuentes/src/ldmgreetercomm.c @ 516

Last change on this file since 516 was 516, checked in by mabarracus, 5 years ago

Copy trusty code

File size: 3.7 KB
Line 
1#include <glib.h>
2#include <libintl.h>
3#include <signal.h>
4#include <stdarg.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10#include "ldmplugin.h"
11#include "ldmutils.h"
12#include "ldmgreetercomm.h"
13#include "logging.h"
14
15static GPid greeterpid;
16static GIOChannel *greeterr;
17static GIOChannel *greeterw;
18
19/*
20 * set_greeter_pid
21 */
22void set_greeter_pid(GPid p) {
23    greeterpid = p;
24}
25
26/*
27 * set_greeter_read_channel
28 */
29void set_greeter_read_channel(GIOChannel *gr) {
30    greeterr = gr;
31}
32
33/*
34 * set_greeter_read_channel
35 */
36void set_greeter_write_channel(GIOChannel *gw) {
37    greeterw = gw;
38}
39
40/*
41 * ask_greeter
42 *  Write command to I/O buffer of the greeter
43 *      cmd -- command to write on buffer
44 *
45 *  Return 1 if fails, else 0
46 */
47int ask_greeter(gchar *cmd) {
48    /* Write command to io channel of the greeter */
49    GError *ge = NULL;
50    if(g_io_channel_write_chars(greeterw, cmd, -1, NULL, &ge) != G_IO_STATUS_NORMAL) {
51        log_entry("ldm",4,"%s", ge->message);
52        return 1;
53    }
54
55    /* Flush buffer */
56    if(g_io_channel_flush(greeterw, &ge) != G_IO_STATUS_NORMAL) {
57        log_entry("ldm",4,"%s", ge->message);
58        return 1;
59    }
60    return 0;
61}
62
63
64/*
65 * close_greeter
66 *  Close greeter properly
67 *      s_ldm -- struct ldm_info
68 */
69void close_greeter() {
70    gboolean failed = 0;
71    gchar *cmd = "quit\n";
72
73    if (!greeterpid)
74        return;
75
76    if(ask_greeter(cmd))
77        failed = 1;
78
79    if(failed) {
80        log_entry("ldm",3,"quit command failed. SIGTERM to greeter");
81        if(kill(greeterpid, SIGTERM) < 0) {
82            log_entry("ldm",3,"SIGTERM failed. SIGKILL to greeter");
83            kill(greeterpid, SIGKILL);
84        }
85    }
86
87    ldm_wait(greeterpid);
88    g_io_channel_shutdown(greeterr, TRUE, NULL);
89    g_io_channel_shutdown(greeterw, TRUE, NULL);
90    close(g_io_channel_unix_get_fd(greeterr));
91    close(g_io_channel_unix_get_fd(greeterw));
92    greeterpid = 0;
93}
94
95/*
96 * set_message
97 *  Ask greeter to print a message to UI
98 */
99int set_message(gchar *msg) {
100    gchar *cmd;
101
102    if (!greeterpid)
103        return 1;
104
105    cmd = g_strconcat("msg ", msg, "\n", NULL);
106    if(ask_greeter(cmd))
107        return 1;
108    g_free(cmd);
109
110    return 0;
111}
112
113/*
114 * listen_greeter
115 *  Read data from I/O buffer of the greeter
116 *      buf -- double ptr to buffer where data will be written
117 *      buflen -- buffer length
118 *      end -- pointer to the end of buffer
119 *
120 *  Return 1 if fails, else 0 and buffer with data in
121 */
122int listen_greeter(gchar **buffer, gsize *buflen, gsize *end) {
123    while (1) {
124        /* Reads data from I/O channel of the greeter */
125        GError *ge = NULL;
126        if(g_io_channel_read_line(greeterr, buffer, buflen, end, &ge) != G_IO_STATUS_NORMAL) {
127            log_entry("ldm",3,"%s", ge->message);
128            return 1;
129        }
130
131        g_strstrip(*buffer);
132        log_entry("ldm",7,"Got command: %s",*buffer);
133
134        /* handle callbacks */
135        if (**buffer == '@') {
136            if (!g_ascii_strncasecmp(*buffer, "@backend@", 9)) {
137                ldm_raise_auth_except(AUTH_EXC_RELOAD_BACKEND);
138                continue;
139            } else if (!g_ascii_strncasecmp(*buffer, "@guest@", 7)) {
140                ldm_raise_auth_except(AUTH_EXC_GUEST);
141                continue;
142            }
143        }
144        break;
145    }
146    return 0;
147}
148
149/*
150 * ask_value_greeter
151 *  Send cmd and expect value
152 *      s_ldm -- ldm_info struct
153 *      cmd -- command to send
154 *      buffer -- buffer to write to answer
155 */
156gchar *ask_value_greeter(gchar *cmd) {
157    gsize len, end;
158    gchar *buffer;
159    if(ask_greeter(cmd))
160        die("ldm", "%s from greeter failed", cmd);
161
162    if(listen_greeter(&buffer, &len, &end))
163        die("ldm", "%s from greeter failed", cmd);
164
165    return buffer;
166}
Note: See TracBrowser for help on using the repository browser.