File: device.c

package info (click to toggle)
zapping 0.10~cvs6-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,828 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,816; xml: 2,742; makefile: 1,282; perl: 488
file content (369 lines) | stat: -rw-r--r-- 7,467 bytes parent folder | download | duplicates (6)
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
/*
 * Zapping (TV viewer for the Gnome Desktop)
 *
 * Copyright (C) 2003 Michael H. Schimek
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: device.c,v 1.11 2006/05/14 13:48:02 mschimek Exp $ */

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>		/* LONG_MAX */
#include <assert.h>
#include <inttypes.h>

#include <fcntl.h>		/* open() */
#include <unistd.h>		/* close(), mmap(), munmap() */
#include <sys/ioctl.h>		/* ioctl() */
#include <sys/mman.h>		/* mmap(), munmap() */
#include <errno.h>

#ifndef PRId64
#  define PRId64 "lld"
#endif
#ifndef PRIu64
#  define PRIu64 "llu"
#endif
#ifndef PRIx64
#  define PRIx64 "llx"
#endif

#include "device.h"

void
fprint_symbolic			(FILE *			fp,
				 int			mode,
				 unsigned long		value,
				 ...)
{
	unsigned int i, j = 0;
	unsigned long v;
	const char *s;
	va_list ap;

	if (mode == 0) {
		unsigned int n[2] = { 0, 0 };

		va_start (ap, value);

		for (i = 0; (s = va_arg (ap, const char *)); i++) {
			v = va_arg (ap, unsigned long);
			n[((v & (v - 1)) == 0)]++; /* single bit */
		}

		mode = 1 + (n[1] > n[0]); /* 1-enum, 2-set flags, 3-all flags */

		va_end (ap); 
	}

	va_start (ap, value);

	for (i = 0; (s = va_arg (ap, const char *)); i++) {
		v = va_arg (ap, unsigned long);
		if (mode == 3 || v == value
		    || (mode == 2 && (v & value))) {
			fprintf (fp, "%s%s%s", j++ ? "|" : "",
				 (mode == 3 && (v & value) == 0) ? "!" : "", s);
			value &= ~v;
		}
	}

	if (0 == value && 0 == j)
		fputc ('0', fp);
	else if (value)
		fprintf (fp, "%s0x%lx", j ? "|" : "", value);

	va_end (ap); 
}

void
fprint_unknown_ioctl		(FILE *			fp,
				 unsigned int		cmd,
				 void *			arg)
{
  fprintf (fp, "<unknown cmd 0x%x %c%c arg=%p size=%u>",
           cmd, IOCTL_READ (cmd) ? 'R' : 'r',
	   IOCTL_WRITE (cmd) ? 'W' : 'w',
	   arg, IOCTL_ARG_SIZE (cmd)); 
}

/**
 * Helper function to calculate remaining time for select().
 * result = timeout - (now - start).
 */
void
timeout_subtract_elapsed	(struct timeval *	result,
				 const struct timeval *	timeout,
				 const struct timeval *	now,
				 const struct timeval *	start)
{
	if (!timeout) {
		result->tv_sec = LONG_MAX;
		result->tv_usec = LONG_MAX;
	} else {
		struct timeval elapsed;

		timeval_subtract (&elapsed, now, start);

		if ((elapsed.tv_sec | elapsed.tv_usec) > 0) {
			timeval_subtract (result, timeout, &elapsed);

			if ((result->tv_sec | result->tv_usec) < 0) {
				result->tv_sec = 0;
				result->tv_usec = 0;
			}
		} else {
			*result = *timeout;
		}
	}
}

int
device_open			(FILE *			fp,
				 const char *		pathname,
				 int			flags,
				 mode_t			mode)
{
  int fd;

  fd = open (pathname, flags, mode);

  if (fp)
    {
      int saved_errno;

      saved_errno = errno;

      fprintf (fp, "%d = open (\"%s\", ", fd, pathname);
      fprint_symbolic (fp, 2, (unsigned long) flags,
		       "RDONLY", O_RDONLY,
		       "WRONLY", O_WRONLY,
		       "RDWR", O_RDWR,
		       "CREAT", O_CREAT,
		       "EXCL", O_EXCL,
		       "TRUNC", O_TRUNC,
		       "APPEND", O_APPEND,
		       "NONBLOCK", O_NONBLOCK,
		       0);
      fprintf (fp, ", 0%o)", mode);

      if (-1 == fd)
	fprintf (fp, ", errno=%d, %s\n",
		 saved_errno, strerror (saved_errno));
      else
	fputc ('\n', fp);

      errno = saved_errno;
    }

  return fd;
}

int
device_close			(FILE *			fp,
				 int			fd)
{
  int r;

  r = close (fd);

  if (fp)
    {
      int saved_errno;

      saved_errno = errno;

      if (-1 == r)
	fprintf (fp, "%d = close (%d), errno=%d, %s\n",
		 r, fd, saved_errno, strerror (saved_errno));
      else
	fprintf (fp, "%d = close (%d)\n", r, fd);

      errno = saved_errno;
    }

  return r;
}

int
device_ioctl			(FILE *			fp,
				 ioctl_log_fn *		fn,
				 int			fd,
				 unsigned int		cmd,
				 void *			arg)
{
  int buf[2048];
  int err;

  if (fp && IOCTL_WRITE (cmd))
    {
      assert (sizeof (buf) >= IOCTL_ARG_SIZE (cmd));
      memcpy (buf, arg, IOCTL_ARG_SIZE (cmd));
    }

  do err = ioctl (fd, cmd, arg);
  while (-1 == err && EINTR == errno);

  if (fp && fn)
    {
      int saved_errno;

      saved_errno = errno;

      fprintf (fp, "%d = ", err);
      fn (fp, cmd, 0, NULL);
      fputc ('(', fp);
      
      if (IOCTL_WRITE (cmd))
	fn (fp, cmd, IOCTL_READ (cmd) ? 2 : 3, &buf);

      if (-1 == err)
	{
	  fprintf (fp, "), errno = %d, %s\n",
		   errno, strerror (errno));
	}
      else 
	{
	  if (IOCTL_READ (cmd))
	    {
	      fputs (") -> (", fp);
	      fn (fp, cmd, IOCTL_WRITE (cmd) ? 1 : 3, arg);
	    }

	  fputs (")\n", fp);
	}

      errno = saved_errno;
    }
  
  return err;
}

void *
device_mmap			(FILE *			fp,
				 void *			start,
				 size_t			length,
				 int			prot,
				 int			flags,
				 int			fd,
				 off_t			offset)
{
  void *r;

  r = mmap (start, length, prot, flags, fd, offset);

  if (fp)
    {
      int saved_errno;

      saved_errno = errno;

      fprintf (fp, "%p = mmap (start=%p length=%d prot=",
	       r, start, (int) length);
      fprint_symbolic (fp, 2, (unsigned long) prot,
		       "EXEC", PROT_EXEC,
		       "READ", PROT_READ,
		       "WRITE", PROT_WRITE,
		       "NONE", PROT_NONE,
		       0);
      fputs (" flags=", fp);
      fprint_symbolic (fp, 2, (unsigned long) flags,
		       "FIXED", MAP_FIXED,
		       "SHARED", MAP_SHARED,
		       "PRIVATE", MAP_PRIVATE,
		       0);
      fprintf (fp, " fd=%d offset=%d)", fd, (int) offset);

      if (MAP_FAILED == r)
	fprintf (fp, ", errno=%d, %s\n",
		 saved_errno, strerror (saved_errno));
      else
	fputc ('\n', fp);

      errno = saved_errno;
    }

  return r;
}

int
device_munmap			(FILE *			fp,
				 void *			start,
				 size_t			length)
{
  int r;

  r = munmap (start, length);

  if (fp)
    {
      int saved_errno;

      saved_errno = errno;

      if (-1 == r)
	fprintf (fp, "%d = munmap (start=%p length=%d), errno=%d, %s\n",
		 r, start, (int) length,
		 saved_errno, strerror (saved_errno));
      else
	fprintf (fp, "%d = munmap (start=%p length=%d)\n",
		 r, start, (int) length);

      errno = saved_errno;
    }

  return r;
}

ssize_t
device_read			(FILE *			fp,
				 int			fd,
				 void *			buf,
				 size_t			count)
{
	ssize_t actual;

	actual = read (fd, buf, count);

	if (fp) {
		int saved_errno;

		saved_errno = errno;

		if (-1 == actual) {
			fprintf (fp, "%" PRId64 " = 0x%" PRIx64
				 " = read (fd=%d buf=%p count=%" PRIu64
				 "=0x%" PRIx64 "), errno=%d, %s\n",
				 (int64_t) actual, (int64_t) actual,
				 fd, buf,
				 (uint64_t) count, (uint64_t) count,
				 saved_errno, strerror (saved_errno));
		} else {
			fprintf (fp, "%" PRId64 " = 0x%" PRIx64
				 " = read (fd=%d buf=%p count=%" PRIu64
				 "=0x%" PRIx64 ")\n",
				 (int64_t) actual, (int64_t) actual,
				 fd, buf,
				 (uint64_t) count, (uint64_t) count);
		}

		errno = saved_errno;
	}

	return actual;
}