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

Last change on this file since 855 was 855, checked in by mabarracus, 5 years ago
  • Updated sources to 2.2.18
  • Ported code from patches to apply in 2.2.18
File size: 3.8 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
23set_greeter_pid(GPid p)
24{
25    greeterpid = p;
26}
27
28/*
29 * set_greeter_read_channel
30 */
31void
32set_greeter_read_channel(GIOChannel * gr)
33{
34    greeterr = gr;
35}
36
37/*
38 * set_greeter_read_channel
39 */
40void
41set_greeter_write_channel(GIOChannel * gw)
42{
43    greeterw = gw;
44}
45
46/*
47 * ask_greeter
48 *  Write command to I/O buffer of the greeter
49 *      cmd -- command to write on buffer
50 *
51 *  Return 1 if fails, else 0
52 */
53int
54ask_greeter(gchar * cmd)
55{
56    /* Write command to io channel of the greeter */
57    GError *ge = NULL;
58    if (g_io_channel_write_chars(greeterw, cmd, -1, NULL, &ge) !=
59        G_IO_STATUS_NORMAL) {
60        log_entry("ldm", 4, "%s", ge->message);
61        return 1;
62    }
63
64    /* Flush buffer */
65    if (g_io_channel_flush(greeterw, &ge) != G_IO_STATUS_NORMAL) {
66        log_entry("ldm", 4, "%s", ge->message);
67        return 1;
68    }
69    return 0;
70}
71
72
73/*
74 * close_greeter
75 *  Close greeter properly
76 *      s_ldm -- struct ldm_info
77 */
78void
79close_greeter()
80{
81    gboolean failed = 0;
82    gchar *cmd = "quit\n";
83
84    if (!greeterpid)
85        return;
86
87    if (ask_greeter(cmd))
88        failed = 1;
89
90    if (failed) {
91        log_entry("ldm", 3, "quit command failed. SIGTERM to greeter");
92        if (kill(greeterpid, SIGTERM) < 0) {
93            log_entry("ldm", 3, "SIGTERM failed. SIGKILL to greeter");
94            kill(greeterpid, SIGKILL);
95        }
96    }
97
98    ldm_wait(greeterpid);
99    g_io_channel_shutdown(greeterr, TRUE, NULL);
100    g_io_channel_shutdown(greeterw, TRUE, NULL);
101    close(g_io_channel_unix_get_fd(greeterr));
102    close(g_io_channel_unix_get_fd(greeterw));
103    greeterpid = 0;
104}
105
106/*
107 * set_message
108 *  Ask greeter to print a message to UI
109 */
110int
111set_message(gchar * msg)
112{
113    gchar *cmd;
114
115    if (!greeterpid)
116        return 1;
117
118    cmd = g_strconcat("msg ", msg, "\n", NULL);
119    if (ask_greeter(cmd))
120        return 1;
121    g_free(cmd);
122
123    return 0;
124}
125
126/*
127 * listen_greeter
128 *  Read data from I/O buffer of the greeter
129 *      buf -- double ptr to buffer where data will be written
130 *      buflen -- buffer length
131 *      end -- pointer to the end of buffer
132 *
133 *  Return 1 if fails, else 0 and buffer with data in
134 */
135int
136listen_greeter(gchar ** buffer, gsize * buflen, gsize * end)
137{
138    while (1) {
139        /* Reads data from I/O channel of the greeter */
140        GError *ge = NULL;
141        if (g_io_channel_read_line(greeterr, buffer, buflen, end, &ge) !=
142            G_IO_STATUS_NORMAL) {
143            log_entry("ldm", 3, "%s", ge->message);
144            return 1;
145        }
146
147        g_strstrip(*buffer);
148        log_entry("ldm", 7, "Got command: %s", *buffer);
149
150        /* handle callbacks */
151        if (**buffer == '@') {
152            if (!g_ascii_strncasecmp(*buffer, "@backend@", 9)) {
153                ldm_raise_auth_except(AUTH_EXC_RELOAD_BACKEND);
154                continue;
155            } else if (!g_ascii_strncasecmp(*buffer, "@guest@", 7)) {
156                ldm_raise_auth_except(AUTH_EXC_GUEST);
157                continue;
158            }
159        }
160        break;
161    }
162    return 0;
163}
164
165/*
166 * ask_value_greeter
167 *  Send cmd and expect value
168 *      s_ldm -- ldm_info struct
169 *      cmd -- command to send
170 *      buffer -- buffer to write to answer
171 */
172gchar *
173ask_value_greeter(gchar * cmd)
174{
175    gsize len, end;
176    gchar *buffer;
177    if (ask_greeter(cmd))
178        die("ldm", "%s from greeter failed", cmd);
179
180    if (listen_greeter(&buffer, &len, &end))
181        die("ldm", "%s from greeter failed", cmd);
182
183    return buffer;
184}
Note: See TracBrowser for help on using the repository browser.