~ubuntu-branches/ubuntu/maverick/ldm/maverick

« back to all changes in this revision

Viewing changes to gtkgreet/ldminfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Graber
  • Date: 2008-11-23 18:23:45 UTC
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20081123182345-l6sewklmk1fxq95n
Tags: upstream-2.0.19
Import upstream version 2.0.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LTSP Graphical GTK Greeter
2
 
 * Copyright (C) 2007 Francis Giraldeau, <francis.giraldeau@revolutionlinux.com>
3
 
 *
4
 
 * - Queries servers to get information about them
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19
 
 */
20
 
 
21
 
 
22
 
#define _GNU_SOURCE
23
 
 
24
 
#include <stdio.h>
25
 
#include <unistd.h>
26
 
#include <stdlib.h>
27
 
#include <gtk/gtk.h>
28
 
#include <glib.h>
29
 
#include <string.h>
30
 
#include <fcntl.h>
31
 
#include <glib/gi18n.h>
32
 
#include <sys/utsname.h>
33
 
#include <sys/types.h>
34
 
#include <net/if.h>
35
 
#include <sys/ioctl.h>
36
 
#include <netdb.h>
37
 
 
38
 
#include <greeter.h>
39
 
 
40
 
void
41
 
ldminfo_init(GHashTable ** ldminfo_hash, GList ** host_list,
42
 
             const char *ldm_server)
43
 
{
44
 
    char **hosts_char = NULL;
45
 
    ldminfo *ldm_host_info = NULL;
46
 
    int i;
47
 
 
48
 
    *ldminfo_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
49
 
                                          g_free, g_free);
50
 
    hosts_char = g_strsplit(ldm_server, " ", -1);
51
 
 
52
 
    for (i = 0; hosts_char != NULL && hosts_char[i] != NULL; i++) {
53
 
        /* Initialize to default values */
54
 
        ldm_host_info = g_new0(ldminfo, 1);
55
 
        ldm_host_info->languages = NULL;
56
 
        ldm_host_info->sessions = NULL;
57
 
        ldm_host_info->rating = 0;
58
 
        ldm_host_info->state = SRV_DOWN;
59
 
 
60
 
        /*
61
 
         * Insert into the hash table.
62
 
         */
63
 
 
64
 
        g_hash_table_insert(*ldminfo_hash, g_strdup(hosts_char[i]),
65
 
                            ldm_host_info);
66
 
 
67
 
        /*
68
 
         * Populate the ldminfo structure, and determine if the host
69
 
         * is up or down.
70
 
         */
71
 
 
72
 
        _ldminfo_query_one(hosts_char[i], ldm_host_info);
73
 
 
74
 
        /*
75
 
         * Add the host to the host list.
76
 
         */
77
 
 
78
 
        *host_list = g_list_append(*host_list, g_strdup(hosts_char[i]));
79
 
    }
80
 
    g_strfreev(hosts_char);
81
 
}
82
 
 
83
 
/*
84
 
 * Do the query for one host and fill ldminfo struct
85
 
 * Note: for right now, we're reading files in /var/run/ldm.  Francis would like
86
 
 * the host detection closer to the login and checking network availability
87
 
 * etc.  What should happen here, for gutsy+1, is to call out to an
88
 
 * external script.  This script will query ldminfo, perform ssh port testing,
89
 
 * etc. Things like nc -z hostname ssh, etc.
90
 
 */
91
 
 
92
 
void
93
 
_ldminfo_query_one(const char *hostname, ldminfo * ldm_host_info)
94
 
{
95
 
    int filedes, numbytes;
96
 
    char buf[MAXBUFSIZE];
97
 
    char hostfile[BUFSIZ];
98
 
 
99
 
    snprintf(hostfile, sizeof hostfile, "/var/run/ldm/%s", hostname);
100
 
 
101
 
    filedes = open(hostfile, O_RDONLY);
102
 
 
103
 
    if ((numbytes = read(filedes, buf, MAXBUFSIZE - 1)) == -1) {
104
 
        perror("read");
105
 
        goto error;
106
 
    }
107
 
 
108
 
    buf[numbytes] = '\0';
109
 
 
110
 
    close(filedes);
111
 
    ldm_host_info->state = SRV_UP;
112
 
    _ldminfo_parse_string(buf, ldm_host_info);
113
 
    return;
114
 
 
115
 
  error:
116
 
    close(filedes);
117
 
    ldm_host_info->state = SRV_DOWN;
118
 
}
119
 
 
120
 
/*
121
 
 * split string by line and then construct the ldm_host_info
122
 
 */
123
 
 
124
 
void
125
 
_ldminfo_parse_string(const char *s, ldminfo * ldm_host_info)
126
 
{
127
 
 
128
 
    char **lines = NULL;
129
 
    int i;
130
 
 
131
 
    lines = g_strsplit(s, "\n", -1);
132
 
 
133
 
    for (i = 0; lines != NULL && lines[i] != NULL; i++) {
134
 
        if (!g_ascii_strncasecmp(lines[i], "language:", 9)) {
135
 
            gchar **val;
136
 
            val = g_strsplit(lines[i], ":", 2);
137
 
            ldm_host_info->languages =
138
 
                g_list_append(ldm_host_info->languages, g_strdup(val[1]));
139
 
            g_strfreev(val);
140
 
        } else if (!g_ascii_strncasecmp(lines[i], "session:", 8)) {
141
 
            gchar **val;
142
 
            val = g_strsplit(lines[i], ":", 2);
143
 
            ldm_host_info->sessions =
144
 
                g_list_append(ldm_host_info->sessions, g_strdup(val[1]));
145
 
            g_strfreev(val);
146
 
        } else if (!g_ascii_strncasecmp(lines[i], "rating:", 7)) {
147
 
            gchar **val;
148
 
            val = g_strsplit(lines[i], ":", 2);
149
 
            ldm_host_info->rating = atoi(val[1]);
150
 
            g_strfreev(val);
151
 
        } else {
152
 
            /* Variable not supported */
153
 
        }
154
 
    }
155
 
    g_strfreev(lines);
156
 
}