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

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

Copy trusty code

File size: 7.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 generate_hash_table(void) {
43    char buffer[1024];
44    FILE * file;
45    char ** ret;
46    display_names = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
47    file = fopen(RC_DIR "/locales", "r");
48    if(file == NULL) {
49        return;
50    }
51    while(fgets(buffer, sizeof(buffer), file) != NULL) {
52        ret = g_strsplit(buffer, " ", 2);
53        g_hash_table_insert(display_names, g_strdup(ret[0]), g_strdup(g_strchomp(ret[1])));
54        g_strfreev(ret);
55    }
56    fclose(file);
57}
58
59static gchar * get_display_name(gchar * locale) {
60    gchar * compare_to;
61    char ** ret;
62    gchar * result;
63    ret = g_strsplit(locale, ".", 2);
64    compare_to = g_strdup(ret[0]);
65    g_strfreev(ret);
66    if(compare_to == NULL) {
67        return g_strdup(locale);
68    }
69    result = g_hash_table_lookup(display_names, compare_to);
70    if(result == NULL) {
71        result = g_strdup(locale);
72    }
73    return result;
74}
75
76/*
77 * ldminfo_free
78 *  ldminfo struct freed
79 */
80void ldminfo_free() {
81    g_hash_table_destroy(ldminfo_hash);
82}
83
84/*
85 * ldminfo_lookup
86 */
87ldminfo *ldminfo_lookup(gconstpointer key) {
88    return g_hash_table_lookup(ldminfo_hash, key);
89}
90
91/*
92 * ldminfo_size
93 */
94int ldminfo_size() {
95    return g_hash_table_size(ldminfo_hash);
96}
97
98/*
99 * ldminfo_init
100 */
101void ldminfo_init(GList **host_list, const char *ldm_server) {
102    char **hosts_char = NULL;
103    ldminfo *ldm_host_info = NULL;
104    int i;
105
106    generate_hash_table();
107
108    /* Static hash table */
109    ldminfo_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
110                                          g_free, g_free);
111    hosts_char = g_strsplit(ldm_server, " ", -1);
112
113    for (i = 0; hosts_char != NULL && hosts_char[i] != NULL; i++) {
114        /* Initialize to default values */
115        ldm_host_info = g_new0(ldminfo, 1);
116        ldm_host_info->languages = NULL;
117        ldm_host_info->session_names = NULL;
118        ldm_host_info->sessions = NULL;
119        ldm_host_info->rating = 0;
120        ldm_host_info->state = SRV_DOWN;
121        ldm_host_info->xsession = NULL;
122
123        /*
124         * Populate the ldminfo structure, and determine if the host
125         * is up or down.
126         */
127
128        _ldminfo_query_one(hosts_char[i], ldm_host_info);
129
130        /*
131         * Insert into the hash table.
132         */
133
134        g_hash_table_insert(ldminfo_hash, g_strdup(hosts_char[i]),
135                            ldm_host_info);
136
137        /*
138         * Add the host to the host list.
139         */
140
141        *host_list = g_list_append(*host_list, g_strdup(hosts_char[i]));
142    }
143    g_strfreev(hosts_char);
144}
145
146/*
147 * Do the query for one host and fill ldminfo struct
148 * Note: for right now, we're reading files in /var/run/ldm.  Francis would like
149 * the host detection closer to the login and checking network availability
150 * etc.  What should happen here, for gutsy+1, is to call out to an
151 * external script.  This script will query ldminfo, perform ssh port testing,
152 * etc. Things like nc -z hostname ssh, etc.
153 */
154void _ldminfo_query_one(const char *hostname, ldminfo * ldm_host_info) {
155    int filedes, numbytes;
156    char buf[MAXBUFSIZE];
157    char hostfile[BUFSIZ];
158
159    snprintf(hostfile, sizeof hostfile, "/var/run/ldm/%s", hostname);
160
161    filedes = open(hostfile, O_RDONLY);
162
163    if ((numbytes = read(filedes, buf, MAXBUFSIZE - 1)) == -1) {
164        perror("read");
165        goto error;
166    }
167
168    buf[numbytes] = '\0';
169
170    close(filedes);
171    ldm_host_info->state = SRV_UP;
172    _ldminfo_parse_string(buf, ldm_host_info);
173    return;
174
175  error:
176    close(filedes);
177    ldm_host_info->state = SRV_DOWN;
178}
179
180/*
181 * split string by line and then construct the ldm_host_info
182 */
183void _ldminfo_parse_string(const char *s, ldminfo * ldm_host_info) {
184    char **lines = NULL;
185    int i;
186
187    lines = g_strsplit(s, "\n", -1);
188
189    for (i = 0; lines != NULL && lines[i] != NULL; i++) {
190        if (!g_ascii_strncasecmp(lines[i], "language:", 9)) {
191            gchar **val;
192            val = g_strsplit(lines[i], ":", 2);
193            ldm_host_info->languages =
194                g_list_append(ldm_host_info->languages, g_strdup(val[1]));
195            ldm_host_info->language_names =
196                g_list_append(ldm_host_info->language_names, get_display_name(val[1]));
197            g_strfreev(val);
198        } else if (!g_ascii_strncasecmp(lines[i], "session:", 8) && !g_strstr_len(s, -1, "session-with-name")) {
199            gchar **val;
200            gchar *name;
201            val = g_strsplit(lines[i], ":", 2);
202            name = g_strrstr(val[1], "/");
203            if(name) {
204                name++;
205            } else {
206                name = val[1];
207            }
208            ldm_host_info->sessions = g_list_append(ldm_host_info->sessions, g_strdup(val[1]));
209            ldm_host_info->session_names = g_list_append(ldm_host_info->session_names, g_strdup(name));
210            g_strfreev(val);
211        } else if (!g_ascii_strncasecmp(lines[i], "session-with-name:", 8)) {
212            gchar **val;
213            val = g_strsplit(lines[i], ":", 3);
214            ldm_host_info->sessions = g_list_append(ldm_host_info->sessions, g_strdup(val[2]));
215            ldm_host_info->session_names = g_list_append(ldm_host_info->session_names, g_strdup(val[1]));
216            g_strfreev(val);
217        } else if (!g_ascii_strncasecmp(lines[i], "rating:", 7)) {
218            gchar **val;
219            val = g_strsplit(lines[i], ":", 2);
220            ldm_host_info->rating = atoi(val[1]);
221            g_strfreev(val);
222        } else if (!g_ascii_strncasecmp(lines[i], "xsession:", 9)) {
223            gchar **val;
224            val = g_strsplit(lines[i], ":", 2);
225            ldm_host_info->xsession = g_strdup(val[1]);
226            g_strfreev(val);
227        } else {
228            /* Variable not supported */
229        }
230    }
231    g_strfreev(lines);
232}
233
234
235/*
236 * ldm_getenv_bool
237 *  Return if env variable is set to true or false
238 *      name -- env. variable name
239 */
240int ldm_getenv_bool(const char *name) {
241    char *env = getenv(name);
242
243    if(env) {
244        if (*env == 'y' || *env == 't' || *env == 'T' || *env == 'Y')
245            return 1;
246    }
247    return 0;
248}
249
250/*
251 * ldm_getenv_int
252 *  Return an int, will return default_value if not set
253 */
254int ldm_getenv_int(const char *name,int default_value) {
255    char *env = getenv(name);
256
257    if(env) {
258        return atoi(env);
259    }
260    return default_value;
261}
Note: See TracBrowser for help on using the repository browser.