-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.c
219 lines (180 loc) · 6.19 KB
/
trace.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
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
/*---------------------------------------------------------------------------*/
/*trace.h*/
/*---------------------------------------------------------------------------*/
/*The definitions for tracing the translator stack under ourselves.*/
/*---------------------------------------------------------------------------*/
/*Copyright (C) 2001, 2002, 2005, 2008 Free Software Foundation, Inc.
Written by Sergiu Ivanov <[email protected]>.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.*/
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
#define _GNU_SOURCE 1
/*---------------------------------------------------------------------------*/
#include <unistd.h>
#include <fcntl.h>
#include <hurd/fsys.h>
/*---------------------------------------------------------------------------*/
#include "debug.h"
#include "trace.h"
#include "node.h"
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*--------Functions----------------------------------------------------------*/
/*Traces the translator stack on the given underlying node until it
finds the first translator called `name` and returns the port
pointing to the translator sitting under this one.*/
error_t
trace_find
(mach_port_t underlying, const char *name, int flags, mach_port_t * port)
{
error_t err = 0;
/*Identity information about the current process */
uid_t *uids;
size_t nuids;
gid_t *gids;
size_t ngids;
/*The name and arguments of the translator being passed now */
char *argz = NULL;
size_t argz_len = 0;
/*The current working directory */
char *cwd = NULL;
/*The port to the directory containing the file pointed to by `underlying` */
file_t dir;
/*The unauthenticated version of `dir` */
file_t unauth_dir;
/*The control port of the current translator */
fsys_t fsys;
/*The port to the translator we are currently looking at */
mach_port_t node = underlying;
/*The port to the previous translator */
mach_port_t prev_node = MACH_PORT_NULL;
/*The retry name and retry type returned by fsys_getroot */
string_t retry_name;
retry_type retry;
/*Finalizes the execution of this function */
void finalize (void)
{
/*If there is a working directory, free it */
if (cwd)
free (cwd);
/*If the ports to the directory exist */
if (dir)
PORT_DEALLOC (dir);
} /*finalize */
/*Obtain the current working directory */
cwd = getcwd (NULL, 0);
if (!cwd)
{
LOG_MSG ("trace_find: Could not obtain cwd.");
return EINVAL;
}
LOG_MSG ("trace_find: cwd: '%s'", cwd);
/*Open a port to this directory */
dir = file_name_lookup (cwd, 0, 0);
if (!dir)
{
finalize ();
return ENOENT;
}
/*Try to get the number of effective UIDs */
nuids = geteuids (0, 0);
if (nuids < 0)
{
finalize ();
return EINVAL;
}
/*Allocate some memory for the UIDs on the stack */
uids = alloca (nuids * sizeof (uid_t));
/*Fetch the UIDs themselves */
nuids = geteuids (nuids, uids);
if (nuids < 0)
{
finalize ();
return EINVAL;
}
/*Try to get the number of effective GIDs */
ngids = getgroups (0, 0);
if (ngids < 0)
{
finalize ();
return EINVAL;
}
/*Allocate some memory for the GIDs on the stack */
gids = alloca (ngids * sizeof (gid_t));
/*Fetch the GIDs themselves */
ngids = getgroups (ngids, gids);
if (ngids < 0)
{
finalize ();
return EINVAL;
}
/*Obtain the unauthenticated version of `dir` */
err = io_restrict_auth (dir, &unauth_dir, 0, 0, 0, 0);
if (err)
{
finalize ();
return err;
}
char buf[256];
char *_buf = buf;
size_t len = 256;
io_read (node, &_buf, &len, 0, len);
LOG_MSG ("trace_find: Read from underlying: '%s'", buf);
/*Go up the translator stack */
for (; !err;)
{
/*retreive the name and options of the current translator */
err = file_get_fs_options (node, &argz, &argz_len);
if (err)
break;
LOG_MSG ("trace_find: Obtained translator '%s'", argz);
if (strcmp (argz, name) == 0)
{
LOG_MSG ("trace_find: Match. Stopping here.");
break;
}
/*try to fetch the control port for this translator */
err = file_get_translator_cntl (node, &fsys);
LOG_MSG ("trace_find: err = %d", (int) err);
if (err)
break;
LOG_MSG ("trace_find: Translator control port: %lu",
(unsigned long) fsys);
prev_node = node;
/*fetch the root of the translator */
err = fsys_getroot
(fsys, unauth_dir, MACH_MSG_TYPE_COPY_SEND,
uids, nuids, gids, ngids,
flags | O_NOTRANS, &retry, retry_name, &node);
LOG_MSG ("trace_find: fsys_getroot returned %d", (int) err);
LOG_MSG ("trace_find: Translator root: %lu", (unsigned long) node);
/*TODO: Remove this debug output. */
/*char buf[256];
char * _buf = buf;
size_t len = 256;
io_read(node, &_buf, &len, 0, len);
LOG_MSG("trace_find: Read: '%s'", buf); */
}
/*If the error occurred (most probably) because of the fact that we
have reached the top of the translator stack */
if ((err == EMACH_SEND_INVALID_DEST) || (err == ENXIO))
/*this is OK */
err = 0;
/*Return the port to read from */
*port = prev_node;
/*Return the result of operations */
finalize ();
return err;
} /*trace_find */
/*---------------------------------------------------------------------------*/