summaryrefslogtreecommitdiff
path: root/boot.backup1/util.h
blob: 9fc3a839f84059d95fcebc8503132aab4f8aa6f8 (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
/* 
   Copyright (C) 2008 Free Software Foundation, Inc.
   Written by Zheng Da.

   This file is part of the GNU Hurd.

   The GNU Hurd 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, or (at your option)
   any later version.

   The GNU Hurd 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 the GNU Hurd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifndef UTIL_H
#define UTIL_H

#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>

#include <mach.h>
#include <hurd.h>

#define DEBUG 0
#define INFO 1
#define LOGLEVEL INFO

#define output(format, ...) do				\
{							\
  char buf[1024];                                       \
  snprintf (buf, 1024, "boot: %s: %s\r\n", __func__, format);       \
  fprintf (stderr , buf, ## __VA_ARGS__);		\
  fflush (stderr);					\
} while (0)

#if DEBUG >= LOGLEVEL
#define debug output
#else
#define debug(format, ...) do {} while (0)
#endif

#if INFO >= LOGLEVEL
#define info output
#else
#define info(format, ...) do {} while (0)
#endif

#if ERROR >= LOGLEVEL
#define logerror(format, ...) do				\
{							\
  char buf[1024];                                       \
  snprintf (buf, 1024, "boot ERROR: %s: %s\r\n", __func__, format);       \
  fprintf (stderr , buf, ## __VA_ARGS__);		\
  fflush (stderr);					\
} while (0)
#else
#define logerror(format, ...) do {} while (0)
#endif

#define print_port_type(port) do				    \
{								    \
  mach_port_type_t ptype;					    \
  error_t err =	mach_port_type (mach_task_self (), port, &ptype);   \
  if (err)							    \
    debug ("mach_port_type: %s", strerror (err));		    \
  else if (ptype & MACH_PORT_TYPE_SEND)				    \
    debug ("port %d has send right", port);			    \
  else if (ptype & MACH_PORT_TYPE_RECEIVE)			    \
    debug ("port %d has receive right", port);			    \
  else if (ptype & MACH_PORT_TYPE_SEND_ONCE)			    \
    debug ("port %d has send once right", port);		    \
  else if (ptype & MACH_PORT_TYPE_PORT_SET)			    \
    debug ("port %d is port set", port);			    \
  else if (ptype & MACH_PORT_TYPE_DEAD_NAME)			    \
    debug ("port %d is a dead name", port);			    \
  else								    \
    debug ("the type of port %d is %d", port, ptype);		    \
  mach_port_urefs_t refs;					    \
  err = mach_port_get_refs (mach_task_self (), port,	    \
			    MACH_PORT_RIGHT_SEND, &refs);	    \
  if (!err)							    \
    debug ("port send right references: %d", refs);		    \
} while (0)

#endif