-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.c
165 lines (128 loc) · 3.55 KB
/
util.c
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
/*
* Copyright (c) 2012 Citrix Systems, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "project.h"
void helper_exec(const char *bin, int domid)
{
char buff[12]={0};
struct stat s;
pid_t pid;
if (stat(bin, &s) == -1)
return;
snprintf(buff,sizeof(buff)-1,"%d",domid);
switch ((pid=fork())) { //wait is mopped up by sigchld in main
case 0:
execl(bin,bin,buff,(char *) 0);
error("execl failed for %s",bin);
_exit(1);
break;
case -1:
error("fork failed");
break;
}
waitpid(pid,NULL,0);
}
static void
bt (void)
{
#if 0
#if 0
unsigned int level = 0;
void *addr;
Dl_info info;
for (;;)
{
addr = __builtin_return_address (level);
if (!addr)
return;
fprintf (stderr, "%d: %p", level, addr);
if (dladdr (addr, &info))
{
char *base, *offset;
base = info.dli_saddr;
offset = addr;
fprintf (stderr, "(%s %s+0x%x)", info.dli_fname, info.dli_sname,
(unsigned int) (offset - base));
}
fprintf (stderr, "\n");
level++;
}
#else
void *ba[256];
Dl_info info;
int i;
int n = backtrace (ba, sizeof (ba) / sizeof (ba[0]));
if (!n)
return;
for (i = 0; i < n; ++i)
{
fprintf (stderr, "%d: %p", i, ba[i]);
if (dladdr (ba[i], &info))
{
char *base, *offset;
base = info.dli_saddr;
offset = ba[i];
fprintf (stderr, "(%s %s+0x%x)", info.dli_fname, info.dli_sname,
(unsigned int) (offset - base));
}
fprintf (stderr, "\n");
}
#endif
#endif
}
void
message (int flags, const char *file, const char *function, int line,
const char *fmt, ...)
{
char buf[1024]={0};
const char *level = "Info";
va_list ap;
int len;
if (flags & MESSAGE_WARNING)
{
level = "Warning";
}
else if (flags & MESSAGE_ERROR)
{
level = "Error";
}
else if (flags & MESSAGE_FATAL)
{
level = "Fatal";
}
va_start (ap, fmt);
len=vsnprintf(buf,sizeof(buf)-1,fmt,ap);
va_end (ap);
if ((len>0) &&(buf[len-1]=='\n')) buf[len-1]=0;
syslog(LOG_ERR, "%s:%s:%s:%d:%s", level, file, function, line,buf);
if (flags & MESSAGE_FATAL)
{
bt ();
abort ();
}
}
void log_dbus_error (const char *file, const char *function, int line, const char *err, const char *fmt, ...)
{
char buf[128]={0};
va_list ap;
int len;
va_start (ap, fmt);
len=vsnprintf(buf,sizeof(buf)-1,fmt,ap);
va_end (ap);
if ((len>0) &&(buf[len-1]=='\n')) buf[len-1]=0;
syslog(LOG_ERR, "Info:%s:%s:%d threw error %s : %s", file, function, line,err,buf);
}