source: ldm/trunk/fuentes/src/ldminfo.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: 8.5 KB
Line 
1/* LTSP Graphical GTK Greeter
2 * Copyright (C) 2007 Francis Giraldeau, <francis.giraldeau@revolutionlinux.com>
3 * Copyright 2007-2008 Scott Balneaves <sbalneav@ltsp.org>
4 * Copyright 2008-2009 Ryan Niebur <ryanryan52@gmail.com>
5 *
6 * - Queries servers to get information about them
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 2 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, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23#define _GNU_SOURCE
24
25#include <fcntl.h>
26#include <glib.h>
27#include <net/if.h>
28#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35
36#include "ldminfo.h"
37#include "ldmutils.h"
38
39static GHashTable *display_names;
40static GHashTable *ldminfo_hash = NULL;
41
42static void
43generate_hash_table(void)
44{
45    char buffer[1024];
46    FILE *file;
47    char **ret;
48    display_names =
49        g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
50    file = fopen(RC_DIR "/locales", "r");
51    if (file == NULL) {
52        return;
53    }
54    while (fgets(buffer, sizeof(buffer), file) != NULL) {
55        ret = g_strsplit(buffer, " ", 2);
56        g_hash_table_insert(display_names, g_strdup(ret[0]),
57                            g_strdup(g_strchomp(ret[1])));
58        g_strfreev(ret);
59    }
60    fclose(file);
61}
62
63static gchar *
64get_display_name(gchar * locale)
65{
66    gchar *compare_to;
67    char **ret;
68    gchar *result;
69    ret = g_strsplit(locale, ".", 2);
70    compare_to = g_strdup(ret[0]);
71    g_strfreev(ret);
72    if (compare_to == NULL) {
73        return g_strdup(locale);
74    }
75    result = g_hash_table_lookup(display_names, compare_to);
76    if (result == NULL) {
77        result = g_strdup(locale);
78    }
79    return result;
80}
81
82/*
83 * ldminfo_free
84 *  ldminfo struct freed
85 */
86void
87ldminfo_free()
88{
89    g_hash_table_destroy(ldminfo_hash);
90}
91
92/*
93 * ldminfo_lookup
94 */
95ldminfo *
96ldminfo_lookup(gconstpointer key)
97{
98    return g_hash_table_lookup(ldminfo_hash, key);
99}
100
101/*
102 * ldminfo_size
103 */
104int
105ldminfo_size()
106{
107    return g_hash_table_size(ldminfo_hash);
108}
109
110/*
111 * ldminfo_init
112 */
113void
114ldminfo_init(GList ** host_list, const char *ldm_server)
115{
116    char **hosts_char = NULL;
117    ldminfo *ldm_host_info = NULL;
118    int i;
119
120    generate_hash_table();
121
122    /* Static hash table */
123    ldminfo_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
124                                         g_free, g_free);
125    hosts_char = g_strsplit(ldm_server, " ", -1);
126
127    for (i = 0; hosts_char != NULL && hosts_char[i] != NULL; i++) {
128        /* Initialize to default values */
129        ldm_host_info = g_new0(ldminfo, 1);
130        ldm_host_info->languages = NULL;
131        ldm_host_info->session_names = NULL;
132        ldm_host_info->sessions = NULL;
133        ldm_host_info->rating = 0;
134        ldm_host_info->state = SRV_DOWN;
135        ldm_host_info->xsession = NULL;
136
137        /*
138         * Populate the ldminfo structure, and determine if the host
139         * is up or down.
140         */
141
142        _ldminfo_query_one(hosts_char[i], ldm_host_info);
143
144        /*
145         * Insert into the hash table.
146         */
147
148        g_hash_table_insert(ldminfo_hash, g_strdup(hosts_char[i]),
149                            ldm_host_info);
150
151        /*
152         * Add the host to the host list.
153         */
154
155        *host_list = g_list_append(*host_list, g_strdup(hosts_char[i]));
156    }
157    g_strfreev(hosts_char);
158}
159
160/*
161 * Do the query for one host and fill ldminfo struct
162 * Note: for right now, we're reading files in /var/run/ldm.  Francis would like
163 * the host detection closer to the login and checking network availability
164 * etc.  What should happen here, for gutsy+1, is to call out to an
165 * external script.  This script will query ldminfo, perform ssh port testing,
166 * etc. Things like nc -z hostname ssh, etc.
167 */
168void
169_ldminfo_query_one(const char *hostname, ldminfo * ldm_host_info)
170{
171    int filedes, numbytes;
172    char buf[MAXBUFSIZE];
173    char hostfile[BUFSIZ];
174
175    snprintf(hostfile, sizeof hostfile, "/var/run/ldm/%s", hostname);
176
177    filedes = open(hostfile, O_RDONLY);
178
179    if ((numbytes = read(filedes, buf, MAXBUFSIZE - 1)) == -1) {
180        perror("read");
181        goto error;
182    }
183
184    buf[numbytes] = '\0';
185
186    close(filedes);
187    ldm_host_info->state = SRV_UP;
188    _ldminfo_parse_string(buf, ldm_host_info);
189    return;
190
191  error:
192    close(filedes);
193    ldm_host_info->state = SRV_DOWN;
194}
195
196/*
197 * split string by line and then construct the ldm_host_info
198 */
199void
200_ldminfo_parse_string(const char *s, ldminfo * ldm_host_info)
201{
202    char **lines = NULL;
203    int i;
204
205    lines = g_strsplit(s, "\n", -1);
206
207    for (i = 0; lines != NULL && lines[i] != NULL; i++) {
208        if (!g_ascii_strncasecmp(lines[i], "language:", 9)) {
209            gchar **val;
210            val = g_strsplit(lines[i], ":", 2);
211            ldm_host_info->languages =
212                g_list_append(ldm_host_info->languages, g_strdup(val[1]));
213            ldm_host_info->language_names =
214                g_list_append(ldm_host_info->language_names,
215                              get_display_name(val[1]));
216            g_strfreev(val);
217        } else if (!g_ascii_strncasecmp(lines[i], "session:", 8)
218                   && !g_strstr_len(s, -1, "session-with-name")) {
219            gchar **val;
220            gchar *name;
221            val = g_strsplit(lines[i], ":", 2);
222            name = g_strrstr(val[1], "/");
223            if (name) {
224                name++;
225            } else {
226                name = val[1];
227            }
228            ldm_host_info->sessions =
229                g_list_append(ldm_host_info->sessions, g_strdup(val[1]));
230            ldm_host_info->session_names =
231                g_list_append(ldm_host_info->session_names,
232                              g_strdup(name));
233            g_strfreev(val);
234        } else if (!g_ascii_strncasecmp(lines[i], "session-with-name:", 8)) {
235            gchar **val;
236            val = g_strsplit(lines[i], ":", 3);
237            ldm_host_info->sessions =
238                g_list_append(ldm_host_info->sessions, g_strdup(val[2]));
239            ldm_host_info->session_names =
240                g_list_append(ldm_host_info->session_names,
241                              g_strdup(val[1]));
242            g_strfreev(val);
243        } else if (!g_ascii_strncasecmp(lines[i], "rating:", 7)) {
244            gchar **val;
245            val = g_strsplit(lines[i], ":", 2);
246            ldm_host_info->rating = atoi(val[1]);
247            g_strfreev(val);
248        } else if (!g_ascii_strncasecmp(lines[i], "xsession:", 9)) {
249            gchar **val;
250            val = g_strsplit(lines[i], ":", 2);
251            ldm_host_info->xsession = g_strdup(val[1]);
252            g_strfreev(val);
253        } else {
254            /* Variable not supported */
255        }
256    }
257    g_strfreev(lines);
258}
259
260
261/*
262 * ldm_getenv_bool
263 *  Return if env variable is set to true or false
264 *      name -- env. variable name
265 */
266int
267ldm_getenv_bool(const char *name)
268{
269    char *env = getenv(name);
270
271    if (env) {
272        if (*env == 'y' || *env == 't' || *env == 'T' || *env == 'Y')
273            return 1;
274    }
275    return 0;
276}
277
278/*
279 * ldm_getenv_bool_default
280 *  Return if env variable is set to true or false
281 *      name -- env. variable name
282 *      default_value -- int to return as default [0,1]
283 */
284int
285ldm_getenv_bool_default(const char *name, const int default_value)
286{
287    char *env = getenv(name);
288
289    if (env != NULL) {
290        if (*env == 'y' || *env == 't' || *env == 'T' || *env == 'Y') {
291            return 1;
292        } else {
293            return 0;
294        }
295    }   
296    return default_value;
297}
298
299/*
300 * ldm_getenv_int
301 *  Return an int, will return default_value if not set
302 */
303int
304ldm_getenv_int(const char *name, int default_value)
305{
306    char *env = getenv(name);
307
308    if (env) {
309        return atoi(env);
310    }
311    return default_value;
312}
313
314/*
315 * ldm_getenv_str_default
316 *  Return a string, will return default_value if not set
317 *  No malloc()s, caller should strdup the result if needed.
318 */
319const char *
320ldm_getenv_str_default(const char *name, const char *default_value)
321{
322    char *env = getenv(name);
323
324    if (env) {
325        return env;
326    }
327    return default_value;
328}
Note: See TracBrowser for help on using the repository browser.