File: connection.c

package info (click to toggle)
viewglob 2.0.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,740 kB
  • ctags: 968
  • sloc: ansic: 13,940; sh: 897; makefile: 315
file content (223 lines) | stat: -rw-r--r-- 4,896 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
	Copyright (C) 2004, 2005 Stephen Bach
	This file is part of the Viewglob package.

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

	Viewglob 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 General Public License for more details.

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

#include "common.h"
#include "connection.h"
#include "actions.h"
#include "hardened-io.h"

#include <string.h>


/* Initialize the given Connection. */
void connection_init(Connection* cnct, gchar* name, int fd_in, int fd_out,
		gchar* buf, gsize buflen, enum process_level pl) {

	g_return_if_fail(cnct != NULL);
	g_return_if_fail(name != NULL);
	g_return_if_fail(fd_in >= 0);
	g_return_if_fail(fd_out >= 0);
	g_return_if_fail(buf != NULL);

	cnct->name = name;
	cnct->fd_in = fd_in;
	cnct->fd_out = fd_out;
	cnct->buf = buf;
	cnct->size = buflen;
	cnct->filled = 0;
	cnct->pos = 0;
	cnct->seglen = 0;
	cnct->pl = pl;
	cnct->status = MS_NO_MATCH;
	cnct->holdover = NULL;
	cnct->ho_written = FALSE;
	cnct->skip = 0;

	/* The shell_status is of course only relevant to the user shell. */
	cnct->ss = SS_EXECUTING;
}


/* Free the read/write and holdover buffers. */
void connection_free(Connection* cnct) {

	g_return_if_fail(cnct != NULL);

	if (cnct->holdover) {
		g_free(cnct->holdover);
		cnct->holdover = NULL;
	}
}


/* Prepend the holdover from the last read onto the beginning
   of the current buffer. If there is no holdover, just initialize the
   offsets. */
void prepend_holdover(Connection* b) {

	g_return_if_fail(b != NULL);

	if (b->holdover) {
		size_t ho_len;
		ho_len = strlen(b->holdover);

		/* Copy over the holdover and then free it. */
		memcpy(b->buf, b->holdover, ho_len);
		g_free(b->holdover);
		b->holdover = NULL;

		b->filled = ho_len;
		b->pos = 0;
		/* b->seglen has not changed since the create_holdover(). */

		if (b->ho_written)
			b->skip = ho_len;
		else
			b->skip = 0;
	}
	else {
		b->filled = 0;
		b->pos = 0;
		b->seglen = 0;
		b->skip = 0;
	}
}


/* Cut the segment under investigation into a holdover, to be prepended to
   a later buffer. */
void create_holdover(Connection* b, gboolean write_later) {

	g_return_if_fail(b != NULL);
	g_return_if_fail(b->holdover == NULL);

	/* Copy as null-terminated string. */
	b->holdover = g_strndup(b->buf + b->pos, b->seglen);

	if (write_later) {
		/* Writing of this segment is postponed until after the next buffer
		   read. */
		b->filled -= b->seglen;
		b->ho_written = FALSE;
	}
	else
		b->ho_written = TRUE;
}


/* Remove the segment under investigation from the buffer. */
void eat_segment(Connection* b) {

	g_return_if_fail(b != NULL);

	char* start;
	char* end;
	size_t length;

	start = b->buf + b->pos;
	end = b->buf + b->pos + b->seglen + 1;
	length = b->filled - (b->pos + b->seglen + 1);

	memmove(start, end, length);

	b->filled -= b->seglen + 1;
	b->seglen = 0;
	/* b->pos is in the right spot because of the eat. */
}


/* Skip the current segment. */
void pass_segment(Connection* b) {

	g_return_if_fail(b != NULL);

	b->pos += b->seglen + 1;
	b->seglen = 0;
}


/* Read in from the connection. */
gboolean connection_read(Connection* cnct) {

	g_return_val_if_fail(cnct != NULL, FALSE);
	g_return_val_if_fail(cnct->filled < cnct->size, FALSE);

	gssize nread;
	gboolean ok = TRUE;

	switch (hardened_read(cnct->fd_in, cnct->buf + cnct->filled,
				cnct->size - cnct->filled, &nread)) {
		case IOR_OK:
			cnct->filled += nread;
			break;

		case IOR_ERROR:
			if (errno == EIO) {
				/* We often get an EIO here when the user shell exits.  For
				   now we pretend it's a gentle error.  FIXME, though, as
				   there's gotta be a side effect. */
				action_queue(A_EXIT);
			}
			else {
				g_critical("Read problem from %s: %s", cnct->name,
						g_strerror(errno));
				ok = FALSE;
			}
			break;

		case IOR_EOF:
			action_queue(A_EXIT);
			break;

		default:
			g_return_val_if_reached(FALSE);
	}

	return ok;
}


/* Write out the full (filled) buffer. */
gboolean connection_write(Connection* cnct) {

	g_return_val_if_fail(cnct != NULL, FALSE);

	gboolean ok = TRUE;

	if (cnct->filled) { 
		switch (write_all(cnct->fd_out, cnct->buf + cnct->skip,
					cnct->filled - cnct->skip)) {

			case IOR_OK:
				break;

			case IOR_ERROR:
				g_critical("Problem writing for %s: %s", cnct->name,
						g_strerror(errno));
				ok = FALSE;
				break;

			default:
				g_return_val_if_reached(FALSE);
		}
	}

	return ok;
}