summaryrefslogtreecommitdiff
path: root/lib/open.c
blob: 75b7c4c31f1f98e686dd5dbd35795f39a06dc38e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* $Id$ */

/* Copyright (C) 1998-99 Martin Baulig
   This file is part of LibGTop 1.0.

   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.

   LibGTop is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.

   LibGTop is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
   License for more details.

   You should have received a copy of the GNU Library General Public
   License along with LibGTop; see the file COPYING.LIB.  If not, write
   to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <glibtop.h>
#include <glibtop/open.h>
#include <glibtop/version.h>
#include <glibtop/sysdeps.h>
#include <glibtop/command.h>
#include <glibtop/xmalloc.h>

#include <glibtop/gnuserv.h>

/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */

void
glibtop_open_l (glibtop *server, const char *program_name,
		const unsigned long features, const unsigned flags)
{
	int connect_type;

	server->name = program_name;

	/* It is important to set _GLIBTOP_INIT_STATE_OPEN here when we
	 * do recursive calls to glibtop_init_r (). */

	server->flags |= _GLIBTOP_INIT_STATE_OPEN;

	server->error_method = GLIBTOP_ERROR_METHOD_DEFAULT;

#ifdef DEBUG
	fprintf (stderr, "SIZEOF: %u - %u - %u - %u - %u - %u\n",
		 sizeof (glibtop_command), sizeof (glibtop_response),
		 sizeof (glibtop_mountentry), sizeof (glibtop_union),
		 sizeof (glibtop_sysdeps), sizeof (glibtop_response_union));
#endif

	switch (server->method) {
	case GLIBTOP_METHOD_DIRECT:
		server->features = 0;
		break;
	case GLIBTOP_METHOD_INET:
#ifdef DEBUG
		fprintf (stderr, "Connecting to '%s' port %ld.\n",
			 server->server_host, server->server_port);
#endif
		
		connect_type = glibtop_make_connection
			(server->server_host, server->server_port,
			 &server->socket);
		
#ifdef DEBUG
		fprintf (stderr, "Connect Type is %d.\n", connect_type);
#endif
		
		server->flags |= _GLIBTOP_INIT_STATE_SERVER;
		
		server->features = -1;
		break;
	case GLIBTOP_METHOD_UNIX:
#ifdef DEBUG
		fprintf (stderr, "Connecting to Unix Domain Socket.\n");
#endif

		connect_type = glibtop_make_connection
			("unix", 0, &server->socket);

#ifdef DEBUG		
		fprintf (stderr, "Connect Type is %d.\n", connect_type);
#endif

		server->flags |= _GLIBTOP_INIT_STATE_SERVER;

		server->features = -1;
		break;
	case GLIBTOP_METHOD_PIPE:
#ifdef DEBUG
		fprintf (stderr, "Opening pipe to server (%s).\n",
			 LIBGTOP_SERVER);
#endif

		if (pipe (server->input) || pipe (server->output))
			glibtop_error_io_r (server, "cannot make a pipe");

		server->pid  = fork ();
		
		if (server->pid < 0) {
			glibtop_error_io_r (server, "fork failed");
		} else if (server->pid == 0) {
			close (0); close (1);
			close (server->input [0]); close (server->output [1]);
			dup2 (server->input [1], 1);
			dup2 (server->output [0], 0);
			execl (LIBGTOP_SERVER, "libgtop-server", NULL);
			glibtop_error_io_r (server, "execl (%s)",
					    LIBGTOP_SERVER);
			_exit (2);
		}

		close (server->input [1]);
		close (server->output [0]);

		server->flags |= _GLIBTOP_INIT_STATE_SERVER;
		
		server->features = -1;
		break;
	}

	/* If the server has been started, ask it for its features. */

	if (server->flags & _GLIBTOP_INIT_STATE_SERVER) {
		char version [BUFSIZ], buffer [BUFSIZ];
		glibtop_sysdeps sysdeps;
		size_t size, nbytes;

		/* First check whether the server version is correct. */

		sprintf (version, LIBGTOP_VERSION_STRING,
			 LIBGTOP_VERSION, LIBGTOP_SERVER_VERSION,
			 sizeof (glibtop_command),
			 sizeof (glibtop_response),
			 sizeof (glibtop_union),
			 sizeof (glibtop_sysdeps));

		size = strlen (version) + 1;
	
		glibtop_read_l (server, sizeof (nbytes), &nbytes);

		if (nbytes != size)
			glibtop_error_r (server,
					 "Requested %u bytes but got %u.",
					 size, nbytes);
		
		glibtop_read_l (server, nbytes, buffer);
		
		if (memcmp (version, buffer, size))
			glibtop_error_r (server, "server version is not %s",
					 LIBGTOP_VERSION);

		/* Now ask it for its features. */
		
		glibtop_call_l (server, GLIBTOP_CMND_SYSDEPS, 0, NULL,
				sizeof (glibtop_sysdeps), &sysdeps);
		
		server->features = sysdeps.features;

		memcpy (&server->sysdeps, &sysdeps, sizeof (glibtop_sysdeps));

#ifdef DEBUG
		fprintf (stderr, "Server features are %lu.\n",
			 server->features);
#endif
	}

	/* In any case, we call the open functions of our own sysdeps
	 * directory. */

#ifdef DEBUG
	fprintf (stderr, "Calling sysdeps open function.\n");
#endif
	
	glibtop_init_s (&server, features, flags);
}