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

Revision 20897, 4.7 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: open.c,v 1.1.1.2 2004-10-03 05:00:40 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/open.h>
26#include <glibtop/version.h>
27#include <glibtop/sysdeps.h>
28#include <glibtop/command.h>
29
30#include <glibtop/gnuserv.h>
31
32/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */
33
34void
35glibtop_open_l (glibtop *server, const char *program_name,
36                const unsigned long features, const unsigned flags)
37{
38        int connect_type;
39
40        server->name = program_name;
41
42        /* It is important to set _GLIBTOP_INIT_STATE_OPEN here when we
43         * do recursive calls to glibtop_init_r (). */
44
45        server->flags |= _GLIBTOP_INIT_STATE_OPEN;
46
47        server->error_method = GLIBTOP_ERROR_METHOD_DEFAULT;
48
49#ifdef DEBUG
50        fprintf (stderr, "SIZEOF: %u - %u - %u - %u - %u - %u\n",
51                 sizeof (glibtop_command), sizeof (glibtop_response),
52                 sizeof (glibtop_mountentry), sizeof (glibtop_union),
53                 sizeof (glibtop_sysdeps), sizeof (glibtop_response_union));
54#endif
55
56        switch (server->method) {
57        case GLIBTOP_METHOD_DIRECT:
58                server->features = 0;
59                break;
60        case GLIBTOP_METHOD_INET:
61#ifdef DEBUG
62                fprintf (stderr, "Connecting to '%s' port %ld.\n",
63                         server->server_host, server->server_port);
64#endif
65
66                connect_type = glibtop_make_connection
67                        (server->server_host, server->server_port,
68                         &server->socket);
69
70#ifdef DEBUG
71                fprintf (stderr, "Connect Type is %d.\n", connect_type);
72#endif
73
74                server->flags |= _GLIBTOP_INIT_STATE_SERVER;
75
76                server->features = -1;
77                break;
78        case GLIBTOP_METHOD_UNIX:
79#ifdef DEBUG
80                fprintf (stderr, "Connecting to Unix Domain Socket.\n");
81#endif
82
83                connect_type = glibtop_make_connection
84                        ("unix", 0, &server->socket);
85
86#ifdef DEBUG
87                fprintf (stderr, "Connect Type is %d.\n", connect_type);
88#endif
89
90                server->flags |= _GLIBTOP_INIT_STATE_SERVER;
91
92                server->features = -1;
93                break;
94        case GLIBTOP_METHOD_PIPE:
95#ifdef DEBUG
96                fprintf (stderr, "Opening pipe to server (%s).\n",
97                         LIBGTOP_SERVER);
98#endif
99
100                if (pipe (server->input) || pipe (server->output))
101                        glibtop_error_io_r (server, "cannot make a pipe");
102
103                server->pid  = fork ();
104
105                if (server->pid < 0) {
106                        glibtop_error_io_r (server, "fork failed");
107                } else if (server->pid == 0) {
108                        close (0); close (1);
109                        close (server->input [0]); close (server->output [1]);
110                        dup2 (server->input [1], 1);
111                        dup2 (server->output [0], 0);
112                        execl (LIBGTOP_SERVER, "libgtop-server", NULL);
113                        glibtop_error_io_r (server, "execl (%s)",
114                                            LIBGTOP_SERVER);
115                        _exit (2);
116                }
117
118                close (server->input [1]);
119                close (server->output [0]);
120
121                server->flags |= _GLIBTOP_INIT_STATE_SERVER;
122
123                server->features = -1;
124                break;
125        }
126
127        /* If the server has been started, ask it for its features. */
128
129        if (server->flags & _GLIBTOP_INIT_STATE_SERVER) {
130                char version [BUFSIZ], buffer [BUFSIZ];
131                glibtop_sysdeps sysdeps;
132                size_t size, nbytes;
133
134                /* First check whether the server version is correct. */
135
136                sprintf (version, LIBGTOP_VERSION_STRING,
137                         LIBGTOP_VERSION, LIBGTOP_SERVER_VERSION,
138                         sizeof (glibtop_command),
139                         sizeof (glibtop_response),
140                         sizeof (glibtop_union),
141                         sizeof (glibtop_sysdeps));
142
143                size = strlen (version) + 1;
144
145                glibtop_read_l (server, sizeof (nbytes), &nbytes);
146
147                if (nbytes != size)
148                        glibtop_error_r (server,
149                                         "Requested %u bytes but got %u.",
150                                         size, nbytes);
151
152                glibtop_read_l (server, nbytes, buffer);
153
154                if (memcmp (version, buffer, size))
155                        glibtop_error_r (server, "server version is not %s",
156                                         LIBGTOP_VERSION);
157
158                /* Now ask it for its features. */
159
160                glibtop_call_l (server, GLIBTOP_CMND_SYSDEPS, 0, NULL,
161                                sizeof (glibtop_sysdeps), &sysdeps);
162
163                server->features = sysdeps.features;
164
165                memcpy (&server->sysdeps, &sysdeps, sizeof (glibtop_sysdeps));
166
167#ifdef DEBUG
168                fprintf (stderr, "Server features are %lu.\n",
169                         server->features);
170#endif
171        }
172
173        /* In any case, we call the open functions of our own sysdeps
174         * directory. */
175
176#ifdef DEBUG
177        fprintf (stderr, "Calling sysdeps open function.\n");
178#endif
179
180        glibtop_init_s (&server, features, flags);
181}
Note: See TracBrowser for help on using the repository browser.