Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 * 	All rights reserved.
 *
 * Author: Harti Brandt <harti@freebsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Begemot: libunimsg/sscop/common.c,v 1.5 2005/05/23 11:46:16 brandt_h Exp $
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <assert.h>
#include <fcntl.h>
#include <err.h>

#include <netnatm/unimsg.h>
#include <netnatm/saal/sscop.h>
#include "common.h"

struct timer {
	evTimerID	id;
	struct sscop	*sscop;
	void		(*func)(void *);
};

int useframe;
int sscopframe;
u_int sscop_vflag;
int sscop_fd;
int user_fd;
int loose;
int user_out_fd;
u_int verbose;
#ifndef USE_LIBBEGEMOT
evContext evctx;
#endif
evFileID sscop_h;
evFileID user_h;

/*
 * This function get's called from sscop to put out verbose messages
 */
void 
sscop_verbose(struct sscop *sscop __unused, void *u __unused,
    const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
	va_end(ap);
}
void
verb(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
	va_end(ap);
}

/*
 * Dump a buffer in hex to stderr.
 */
void
dump_buf(const char *w, const u_char *buf, size_t len)
{
	u_int i;

	fprintf(stderr, "%s %zu: ", w, len);
	for(i = 0; i < len; i++) {
		if (i % 4 == 0 && i != 0)
			fprintf(stderr, " ");
		fprintf(stderr, "%02x", *buf++);
	}
	fprintf(stderr, "\n");
}

/*
 * SSCOP file descriptor is ready. Allocate and read one message
 * and dispatch a signal.
 */
struct uni_msg *
proto_msgin(int fd __unused)
{
	struct uni_msg *m = NULL;
	ssize_t size;
	u_int32_t flen;
	u_int got;

	if (sscopframe) {
		if ((size = read(sscop_fd, &flen, 4)) == -1)
			err(1, "error reading frame hdr");
		if (size == 0) {
			got = 0;
			goto eof;
		}
		if (size != 4)
			errx(1, "short frame header: %zd", size);
		if ((m = uni_msg_alloc(flen)) == NULL)
			err(1, NULL);
		for (got = 0; got < flen; got += (size_t)size) {
			size = read(sscop_fd, m->b_rptr + got, flen - got);
			if (size == -1)
				err(1, "error reading frame");
			if (size == 0) {
				got = 0;
				break;
			}
		}

	} else {
		if ((m = uni_msg_alloc(MAXMSG)) == NULL)
			err(1, NULL);
		if ((size = read(sscop_fd, m->b_rptr, MAXMSG)) == -1)
			err(1, "error reading message");
		got = size;
	}

	if (got == 0) {
  eof:
#ifdef USE_LIBBEGEMOT
		poll_unregister(sscop_h);
#else
		evDeselectFD(evctx, sscop_h);
#endif
		(void)close(sscop_fd);
		sscop_fd = -1;
		if (m != NULL)
			uni_msg_destroy(m);
		VERBOSE(("EOF on sscop file descriptor"));
		return (NULL);
	}
	m->b_wptr = m->b_rptr + got;

	if(verbose & 0x0002)
		dump_buf("SSCOP INPUT", m->b_rptr, got);

	return (m);
}

/*
 * User file descriptor ready - read a message
 */
struct uni_msg *
user_msgin(int fd __unused)
{
	struct uni_msg *m = NULL;
	ssize_t size;
	u_int32_t flen;
	u_int got;

	if (useframe) {
		if ((size = read(user_fd, &flen, 4)) == -1)
			err(1, "error reading frame hdr");
		if (size == 0) {
			got = 0;
			goto eof;
		}
		if (size != 4)
			errx(1, "short frame header: %zd", size);
		if ((m = uni_msg_alloc(flen)) == NULL)
			err(1, NULL);
		for (got = 0; got < flen; got++) {
			size = read(user_fd, m->b_rptr + got, flen - got);
			if (size == -1)
				err(1, "error reading frame");
			if (size == 0) {
				got = 0;
				break;
			}
			got += (size_t)size;
		}

	} else {
		if ((m = uni_msg_alloc(MAXMSG)) == NULL)
			err(1, NULL);
		if ((size = read(user_fd, m->b_rptr, MAXMSG)) == -1)
			err(1, "error reading message");
		got = size;
	}

	if (size == 0) {
  eof:
#ifdef USE_LIBBEGEMOT
		poll_unregister(user_h);
#else
		evDeselectFD(evctx, user_h);
#endif
		if (m != NULL)
			uni_msg_destroy(m);
		VERBOSE(("EOF on user connection"));
		return (NULL);
	}
	m->b_wptr = m->b_rptr + size;

	return (m);
}

/*
 * Write message to the SSCOP file descriptor.
 * Here we have a problem: we should have a means to check how much space
 * we have. If the pipe is full, we could declare the lower layer busy and
 * drop the message. However, how do we know, when a message will fit?
 * Selecting for WRITE doesn't help, because it will return even if a single
 * byte can be written. For this reason, we switch the file descriptor to
 * blocking mode, and hope everything is fast enough to not timeout us.
 * Alternatively we could just drop the message. Using kevent would help here.
 */
void
proto_msgout(struct uni_msg *m)
{
	struct iovec iov[2];
	u_int32_t flen;
	ssize_t size;
	static int sent;
	int fl;

	if (verbose & 0x0002)
		dump_buf("send", m->b_rptr, uni_msg_len(m));
	if (loose > 0 && (sent++ % loose) == loose - 1) {
		VERBOSE(("loosing message"));
		uni_msg_destroy(m);
		return;
	}

	flen = uni_msg_len(m);

	iov[0].iov_len = sscopframe ? 4 : 0;
	iov[0].iov_base = (caddr_t)&flen;
	iov[1].iov_len = uni_msg_len(m);
	iov[1].iov_base = m->b_rptr;

	if ((fl = fcntl(sscop_fd, F_GETFL, 0)) == -1)
		err(1, "cannot get flags for sscop fd");
	fl &= ~O_NONBLOCK;
	if (fcntl(sscop_fd, F_SETFL, fl) == -1)
		err(1, "cannot set flags for sscop fd");

	if ((size = writev(sscop_fd, iov, 2)) == -1)
		err(1, "write sscop");
	if ((size_t)size != iov[0].iov_len + iov[1].iov_len)
		err(1, "short sscop write %zu %zu %zd",
		    iov[0].iov_len, iov[1].iov_len, size);

	fl |= O_NONBLOCK;
	if (fcntl(sscop_fd, F_SETFL, fl) == -1)
		err(1, "cannot restore flags for sscop fd");

	uni_msg_destroy(m);
}

/*
 * output a message to the user
 */
void
user_msgout(struct uni_msg *m)
{
	struct iovec iov[2];
	u_int32_t flen;
	ssize_t size;

	flen = uni_msg_len(m);

	iov[0].iov_len = useframe ? 4 : 0;
	iov[0].iov_base = (caddr_t)&flen;
	iov[1].iov_len = uni_msg_len(m);
	iov[1].iov_base = m->b_rptr;

	if ((size = writev(user_out_fd, iov, 2)) == -1)
		err(1, "write sscop");
	if ((size_t)size != iov[0].iov_len + iov[1].iov_len)
		errx(1, "short sscop write");

	uni_msg_destroy(m);
}

void
parse_param(struct sscop_param *param, u_int *pmask, int opt, char *arg)
{
	u_int val;
	char *end, *p;

	if(opt == 'b') {
		param->flags |= SSCOP_ROBUST;
		*pmask |= SSCOP_SET_ROBUST;
		return;
	}
	if(opt == 'x') {
		param->flags |= SSCOP_POLLREX;
		*pmask |= SSCOP_SET_POLLREX;
		return;
	}
	if(opt == 'W') {
		val = (u_int)strtoul(optarg, &end, 0);

		if(*end != '\0')
			errx(1, "bad number to -W '%s'", optarg);
		if(val >= (1 << 24) - 1)
			errx(1, "window too large: 0x%x", val);
		param->mr = val;
		*pmask |= SSCOP_SET_MR;
		return;
	}

	if((p = strchr(arg, '=')) == NULL)
		errx(1, "need '=' in argument to -%c", opt);
	*p++ = 0;
	if(*p == 0)
		errx(1, "argument to -%c %s empty", opt, arg);
	val = strtoul(p, &end, 0);
	if(*end != 0)
		errx(1, "bad number in -%c %s=%s", opt, arg, p);

	if(opt == 't') {
		if(strcmp(arg, "cc") == 0) {
			param->timer_cc = val;
			*pmask |= SSCOP_SET_TCC;
		} else if(strcmp(arg, "poll") == 0) {
			param->timer_poll = val;
			*pmask |= SSCOP_SET_TPOLL;
		} else if(strcmp(arg, "ka") == 0) {
			param->timer_keep_alive = val;
			*pmask |= SSCOP_SET_TKA;
		} else if(strcmp(arg, "nr") == 0) {
			param->timer_no_response = val;
			*pmask |= SSCOP_SET_TNR;
		} else if(strcmp(arg, "idle") == 0) {
			param->timer_idle = val;
			*pmask |= SSCOP_SET_TIDLE;
		} else
			errx(1, "bad timer name '%s'", arg);
		return;
	}

	if(opt == 'a') {
		if(strcmp(arg, "j") == 0) {
			param->maxj = val;
			*pmask |= SSCOP_SET_MAXJ;
		} else if(strcmp(arg, "k") == 0) {
			param->maxk = val;
			*pmask |= SSCOP_SET_MAXK;
		} else if(strcmp(arg, "cc") == 0) {
			param->maxcc = val;
			*pmask |= SSCOP_SET_MAXCC;
		} else if(strcmp(arg, "pd") == 0) {
			param->maxpd = val;
			*pmask |= SSCOP_SET_MAXPD;
		} else if(strcmp(arg, "stat") == 0) {
			param->maxstat = val;
			*pmask |= SSCOP_SET_MAXSTAT;
		} else
			errx(1, "bad parameter '%s'", arg);
		return;
	}

	verb("bad flag");
	abort();
}

#ifdef USE_LIBBEGEMOT
static void
tfunc(int tid __unused, void *uap)
#else
static void
tfunc(evContext ctx __unused, void *uap, struct timespec due __unused,
    struct timespec inter __unused)
#endif
{
	struct timer *t = uap;

	t->func(t->sscop);
	free(t);
}

/*
 * Start a timer
 */
void *
sscop_start_timer(struct sscop *sscop, void *arg __unused, u_int msec,
    void (*func)(void *))
{
	struct timer *t;
#ifndef USE_LIBBEGEMOT
	struct timespec due;
#endif

	if ((t = malloc(sizeof(*t))) == NULL)
		err(1, NULL);
	t->sscop = sscop;
	t->func = func;

#ifdef USE_LIBBEGEMOT
	if ((t->id = poll_start_timer(msec, 0, tfunc, t)) == -1)
		err(1, "cannot start timer");
#else
	due = evAddTime(evNowTime(),
	    evConsTime((time_t)msec/1000, (long)(msec%1000)*1000));

	if (evSetTimer(evctx, tfunc, t, due, evConsTime(0, 0), &t->id))
		err(1, "cannot start timer");
#endif

	return (t);
}

/*
 * Stop a timer
 */
void
sscop_stop_timer(struct sscop *sscop __unused, void *arg __unused, void *tp)
{
	struct timer *t = tp;

#ifdef USE_LIBBEGEMOT
	poll_stop_timer(t->id);
#else
	evClearTimer(evctx, t->id);
#endif
	free(t);
}