source: trunk/third/libgtop/lib/init.c @ 20897

Revision 20897, 5.9 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20896, which included commits to RCS files with non-trunk default branches.
Line 
1/* $Id: init.c,v 1.1.1.2 2004-10-03 04:59:51 ghudson Exp $ */
2
3/* Copyright (C) 1998-99 Martin Baulig
4   This file is part of LibGTop 1.0.
5
6   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
7
8   LibGTop is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   LibGTop is distributed in the hope that it will be useful, but WITHOUT
14   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16   for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LibGTop; see the file COPYING. If not, write to the
20   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21   Boston, MA 02111-1307, USA.
22*/
23
24#include <glibtop.h>
25#include <glibtop/sysdeps.h>
26#include <glibtop/parameter.h>
27
28#ifndef DEFAULT_PORT
29#define DEFAULT_PORT 42800
30#endif
31
32static glibtop _glibtop_global_server = { .flags = 0 };
33glibtop *glibtop_global_server = &_glibtop_global_server;
34
35static void
36_init_server (glibtop *server, const unsigned features)
37{
38        char *command, *temp;
39
40        /* Try to get server command, but don't override if already
41         * set via glibtop_set_parameter () */
42
43        if (server->server_command == NULL) {
44                const char *temp = getenv ("LIBGTOP_SERVER") ?
45                        getenv ("LIBGTOP_SERVER") : LIBGTOP_SERVER;
46
47                server->server_command = g_strdup (temp);
48        }
49
50        if (server->server_rsh == NULL) {
51                const char *temp = getenv ("LIBGTOP_RSH") ?
52                        getenv ("LIBGTOP_RSH") : "/usr/bin/ssh";
53
54                server->server_rsh = g_strdup (temp);
55        }
56
57        /* Try to get server method, but don't override if already
58         * set via  glibtop_set_parameter () */
59
60        if (server->method) return;
61
62        /* If server->command doesn't start with a colon, then it is
63         * the full pathname of the server executable. */
64
65        if (server->server_command [0] != ':') {
66                if (features & glibtop_server_features) {
67                        /* We really need the server. */
68                        server->method = GLIBTOP_METHOD_PIPE;
69                } else {
70                        /* Fine. No server is needed, so we call the
71                         * sysdeps functions directly. */
72                        server->method = GLIBTOP_METHOD_DIRECT;
73                }
74
75                return;
76        }
77
78
79        /* If the first character of 'server_command' is a colon,
80         * the first field is the method to connect to the server. */
81
82        /* Everything up to the next colon is the method. */
83
84        command = g_strdup (server->server_command+1);
85        temp = strstr (command, ":");
86        if (temp) *temp = 0;
87
88        /* Dispatch method. */
89
90        if (!strcmp (command, "direct")) {
91                /* Use sysdeps dir instead of connecting to server
92                 * even if using the server would be required on
93                 * the current system. */
94                server->method = GLIBTOP_METHOD_DIRECT;
95
96        } else if (!strcmp (command, "inet")) {
97
98                server->method = GLIBTOP_METHOD_INET;
99
100                /* Connect to internet server. */
101
102                if (temp == NULL) {
103                        /* If no value was set, we use 'localhost'. */
104                        if (server->server_host == NULL)
105                                server->server_host = g_strdup ("localhost");
106                } else {
107                        char *temp2 = strstr (temp+1, ":");
108                        if (temp2) *temp2 = 0;
109
110                        /* Override default. */
111                        if (server->server_host)
112                                g_free ((char *) server->server_host);
113
114                        server->server_host = g_strdup (temp+1);
115
116                        temp = temp2;
117                }
118
119                if (temp == NULL) {
120                        /* If no value was set, we use DEFAULT_PORT. */
121                        if (server->server_port == 0)
122                                server->server_port = DEFAULT_PORT;
123                } else {
124                        char *temp2 = strstr (temp+1, ":");
125                        if (temp2) *temp2 = 0;
126
127                        if (sscanf (temp+1, "%ld", &server->server_port) != 1)
128                                server->server_port = DEFAULT_PORT;
129
130                        temp = temp2 ? temp2 + 1 : temp2;
131                }
132
133        } else if (!strcmp (command, "unix")) {
134
135                /* Connect to unix domain socket. */
136                server->method = GLIBTOP_METHOD_UNIX;
137
138        } else if (!strcmp (command, "pipe")) {
139
140                /* Open pipe to server. */
141                server->method = GLIBTOP_METHOD_PIPE;
142
143        } else {
144
145                glibtop_error_r (server, "Unknown server method '%s'",
146                                 server->server_command+1);
147
148        }
149
150        g_free (command);
151}
152
153glibtop *
154glibtop_init_r (glibtop **server_ptr, unsigned long features, unsigned flags)
155{
156        glibtop *server;
157
158        if (server_ptr == NULL)
159                return NULL;
160
161        if (*server_ptr == NULL)
162                *server_ptr = glibtop_global_server;
163
164        server = *server_ptr;
165
166        /* Should we do the initialization? */
167
168        if (flags & GLIBTOP_INIT_NO_INIT)
169                return server;
170
171        /* Do the initialization, but only if not already initialized. */
172
173        if ((server->flags & _GLIBTOP_INIT_STATE_INIT) == 0) {
174                if (flags & GLIBTOP_FEATURES_EXCEPT)
175                        features = ~features & GLIBTOP_SYSDEPS_ALL;
176
177                if (features == 0)
178                        features = GLIBTOP_SYSDEPS_ALL;
179
180                if (flags & GLIBTOP_FEATURES_NO_SERVER) {
181                        server->method = GLIBTOP_METHOD_DIRECT;
182                        features = 0;
183                }
184
185                server->features = features;
186
187                _init_server (server, features);
188
189                server->flags |= _GLIBTOP_INIT_STATE_INIT;
190
191                switch (server->method) {
192                case GLIBTOP_METHOD_PIPE:
193                case GLIBTOP_METHOD_UNIX:
194                        if (glibtop_server_features & features)
195                                break;
196
197                        server->method = GLIBTOP_METHOD_DIRECT;
198                        break;
199                }
200        }
201
202        /* Should we open the server? */
203
204        if (flags & GLIBTOP_INIT_NO_OPEN)
205                return server;
206
207        /* Open server, but only if not already opened. */
208
209        if ((server->flags & _GLIBTOP_INIT_STATE_OPEN) == 0)
210                glibtop_open_l (glibtop_global_server, "glibtop",
211                                features, flags);
212
213        return server;
214}
215
216glibtop *
217glibtop_init_s (glibtop **server_ptr, unsigned long features, unsigned flags)
218{
219        glibtop *server;
220        glibtop_init_func_t *init_fkt;
221
222        if (server_ptr == NULL)
223                return NULL;
224
225        if (*server_ptr == NULL)
226                *server_ptr = glibtop_global_server;
227
228        server = *server_ptr;
229
230        /* Should we do the initialization? */
231
232        if (flags & GLIBTOP_INIT_NO_INIT)
233                return server;
234
235        /* Do the initialization, but only if not already initialized. */
236
237        if ((server->flags & _GLIBTOP_INIT_STATE_SYSDEPS) == 0) {
238                glibtop_open_s (server, "glibtop", features, flags);
239
240                for (init_fkt = _glibtop_init_hook_s; *init_fkt; init_fkt++)
241                        (*init_fkt) (server);
242
243                server->flags |= _GLIBTOP_INIT_STATE_SYSDEPS;
244        }
245
246        return server;
247}
Note: See TracBrowser for help on using the repository browser.