821769e5ce58b733556d52524b425754ae283fe7
[nestedvm.git] / src / org / ibex / nestedvm / support_aux.c
1 #include <string.h>
2 #include <sys/stat.h>
3 #include <sys/dirent.h>
4 #include <sys/types.h>
5 #include <utime.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <signal.h>
12
13 int _syscall_set_errno(struct _reent *ptr, int err) {
14     ptr->_errno = -err;
15     return -1;
16 }
17
18 extern int _stat_r(struct _reent *, const char *, struct stat *);
19 int _access_r(struct _reent *ptr, const char *pathname, int mode) {
20     struct stat statbuf;
21     if(_stat_r(ptr,pathname,&statbuf) < 0) return -1;
22     return 0;
23 }
24
25 /* NestedVM doesn't, and probably never will, support this security related stuff */
26 uid_t getuid() { return 0; }
27 gid_t getgid() { return 0; }
28 uid_t geteuid() { return 0; }
29 gid_t getegid() { return 0; }
30 int getgroups(int gidsetlen, gid_t *gidset) {
31     if(gidsetlen) *gidset = 0;
32     return 1;
33 }
34 mode_t umask(mode_t new) { return 0022; }
35 int _chmod_r(struct _reent *ptr, const char *f, mode_t mode) { return 0; }
36 int _fchmod_r(struct _reent *ptr, int fd, mode_t mode) { return 0; }
37 int _chown_r(struct _reent *ptr, const char *f, uid_t uid, gid_t gid) { return 0; }
38 int _fchown_r(struct _reent *ptr, int fd, uid_t uid, gid_t gid) { return 0; }
39
40 #define REENT_WRAPPER0R(f,rt) \
41     extern rt _##f##_r(struct _reent *ptr); \
42     rt f() { return _##f##_r(_REENT); }
43 #define REENT_WRAPPER0(f) REENT_WRAPPER0R(f,int)
44
45 #define REENT_WRAPPER1R(f,rt,t1) \
46     extern rt _##f##_r(struct _reent *ptr, t1 a); \
47     rt f(t1 a) { return _##f##_r(_REENT,a); }
48 #define REENT_WRAPPER1(f,t1) REENT_WRAPPER1R(f,int,t1)
49
50 #define REENT_WRAPPER2R(f,rt,t1,t2) \
51     extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b); \
52     rt f(t1 a, t2 b) { return _##f##_r(_REENT,a,b); }
53 #define REENT_WRAPPER2(f,t1,t2) REENT_WRAPPER2R(f,int,t1,t2)
54
55 #define REENT_WRAPPER3R(f,rt,t1,t2,t3) \
56     extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c); \
57     rt f(t1 a, t2 b, t3 c) { return _##f##_r(_REENT,a,b,c); }
58 #define REENT_WRAPPER3(f,t1,t2,t3) REENT_WRAPPER3R(f,int,t1,t2,t3)
59
60 #define REENT_WRAPPER4R(f,rt,t1,t2,t3,t4) \
61 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d); \
62 rt f(t1 a, t2 b, t3 c, t4 d) { return _##f##_r(_REENT,a,b,c,d); }
63 #define REENT_WRAPPER4(f,t1,t2,t3,t4) REENT_WRAPPER4R(f,int,t1,t2,t3,t4)
64
65 REENT_WRAPPER2(mkdir,const char *,mode_t)
66 REENT_WRAPPER2(access,const char *,int)
67 REENT_WRAPPER1(rmdir,const char *)
68 REENT_WRAPPER1R(sysconf,long,int)
69 REENT_WRAPPER1(chdir,const char*)
70 REENT_WRAPPER2(utime,const char *,const struct utimbuf *)
71 REENT_WRAPPER1(pipe,int *)
72 REENT_WRAPPER2(dup2,int,int)
73 REENT_WRAPPER3(waitpid,pid_t,int *,int)
74 REENT_WRAPPER2R(getcwd,char *,char *,size_t)
75 REENT_WRAPPER2R(_getcwd,char *,char *,size_t)
76 REENT_WRAPPER2(symlink,const char *,const char *)
77 REENT_WRAPPER3(readlink,const char *, char *,int)
78 REENT_WRAPPER3(chown,const char *,uid_t,gid_t)
79 REENT_WRAPPER3(fchown,int,uid_t,gid_t)
80 REENT_WRAPPER3(lchown,const char *,uid_t,gid_t)
81 REENT_WRAPPER2(chmod,const char *,mode_t)
82 REENT_WRAPPER2(fchmod,int,mode_t)
83 REENT_WRAPPER2(lstat,const char *,struct stat *)
84 REENT_WRAPPER4(getdents,int, char *, size_t,long *)
85 REENT_WRAPPER1(dup,int)
86 REENT_WRAPPER2R(pathconf,long,const char *,int)
87 REENT_WRAPPER0(vfork)
88 REENT_WRAPPER1(chroot,const char *)
89 REENT_WRAPPER3(mknod,const char *,mode_t,dev_t)
90 REENT_WRAPPER2(ftruncate,int,off_t)
91 REENT_WRAPPER1(usleep,unsigned int)
92 REENT_WRAPPER2(mkfifo,const char *, mode_t)
93
94 extern int __execve_r(struct _reent *ptr, const char *path, char *const argv[], char *const envp[]);
95 int _execve(const char *path, char *const argv[], char *const envp[]) {
96     return __execve_r(_REENT,path,argv,envp);
97 }
98
99 char *_getcwd_r(struct _reent *ptr, char *buf, size_t size) {
100     if(buf != NULL) {
101         buf = __getcwd_r(ptr,buf,size);
102         return (long)buf == -1 ? NULL : buf;
103     }
104     
105     size = 256;
106     for(;;) {
107         buf = malloc(size);
108         char *ret = __getcwd_r(ptr,buf,size);
109         if((long)ret != -1) return ret;
110         free(buf);
111         size *= 2;
112         if(ptr->_errno != ERANGE) return NULL;
113     }
114 }
115
116 pid_t _wait_r(struct _reent *ptr, int *status) {
117     return _waitpid_r(ptr,-1,status,0);
118 }
119
120 long _pathconf_r(struct _reent *ptr,const char *path, int name) {
121     switch(name) {
122         default:
123             fprintf(stderr,"WARNING: pathconf: Unknown \"name\": %d\n",name);
124             ptr->_errno = EINVAL;
125             return -1;
126     }
127 }
128
129 void sync() {
130     /* do nothing*/
131 }
132
133 int fsync(int fd) {
134     /* do nothing */
135     return 0;
136 }
137
138 char *ttyname(int fd) {
139     return isatty(fd) ? "/dev/console" : NULL;
140 }
141
142 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
143     _sig_func_ptr old;
144     _sig_func_ptr new;
145     if(act) {
146         if(act->sa_flags || act->sa_mask != ~((sigset_t)0)) { errno = EINVAL; return -1; }
147         old = signal(sig,act->sa_handler);
148     } else if(oact) {
149         old = signal(sig,SIG_DFL);
150         signal(sig,old);
151     }
152     if(oact) {
153         oact->sa_handler = old;
154         oact->sa_mask = 0;
155         oact->sa_mask = ~((sigset_t)0);
156     }
157     return 0;
158 }
159
160 int sigfillset(sigset_t *set) {
161     *set = ~((sigset_t)0);
162     return 0;
163 }
164
165 int sigemptyset(sigset_t *set) {
166     *set = (sigset_t) 0;
167     return 0;
168 }
169
170 DIR *opendir(const char *path) {
171     struct stat sb;
172     int fd;
173     DIR *dir;
174     
175     fd = open(path,O_RDONLY);
176     if(fd < 0) return NULL;
177     
178     if(fstat(fd,&sb) < 0 || !S_ISDIR(sb.st_mode)) {
179         close(fd);
180         errno = ENOTDIR;
181         return NULL;
182     }
183     
184     dir = malloc(sizeof(*dir));
185     if(dir == NULL) {
186         close(fd);
187         errno = ENOMEM;
188         return NULL;
189     }
190     dir->dd_fd = fd;
191     dir->dd_buf = malloc(sizeof(struct dirent));
192     dir->dd_size = sizeof(struct dirent);
193     if(dir->dd_buf == NULL) {
194         close(fd);
195         free(dir);
196         return NULL;
197     }
198     dir->dd_loc = 0;
199     dir->dd_len = 0;
200     return dir;
201 }
202
203 struct dirent *readdir(DIR *dir) {
204     struct dirent *dp;
205     errno = 0;
206     if(dir->dd_loc == 0 || dir->dd_loc == dir->dd_len) {
207         dir->dd_len = getdents(dir->dd_fd,dir->dd_buf,dir->dd_size,NULL);
208         dir->dd_loc = 0;
209         if(dir->dd_len <= 0) { dir->dd_len = 0; return NULL; }
210     }
211     dp = (struct dirent*) (dir->dd_buf + dir->dd_loc);
212     if(dp->d_reclen == 0 || dp->d_reclen > dir->dd_len - dir->dd_loc) return NULL;
213     dir->dd_loc += dp->d_reclen;
214     return dp;
215 }
216
217 int closedir(DIR *dir) {
218     int fd = dir->dd_fd;
219     free(dir->dd_buf);
220     free(dir);
221     return close(fd);
222 }
223
224 /*
225  * Other People's Code 
226  */
227
228 /* FreeBSD's dirname/basename */
229
230 /* FIXME: Put these in a header */
231
232 /*
233  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
234  * All rights reserved.
235  *
236  * Redistribution and use in source and binary forms, with or without
237  * modification, are permitted provided that the following conditions
238  * are met:
239  * 1. Redistributions of source code must retain the above copyright
240  *    notice, this list of conditions and the following disclaimer.
241  * 2. Redistributions in binary form must reproduce the above copyright
242  *    notice, this list of conditions and the following disclaimer in the
243  *    documentation and/or other materials provided with the distribution.
244  * 3. The name of the author may not be used to endorse or promote products
245  *    derived from this software without specific prior written permission.
246  *
247  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
248  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
249  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
250  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
251  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
252  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
253  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
254  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
255  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
256  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
257  */
258
259 char *
260 dirname(path)
261 const char *path;
262 {
263     static char bname[MAXPATHLEN];
264     register const char *endp;
265     
266     /* Empty or NULL string gets treated as "." */
267     if (path == NULL || *path == '\0') {
268         (void)strcpy(bname, ".");
269         return(bname);
270     }
271     
272     /* Strip trailing slashes */
273     endp = path + strlen(path) - 1;
274     while (endp > path && *endp == '/')
275         endp--;
276     
277     /* Find the start of the dir */
278     while (endp > path && *endp != '/')
279         endp--;
280     
281     /* Either the dir is "/" or there are no slashes */
282     if (endp == path) {
283         (void)strcpy(bname, *endp == '/' ? "/" : ".");
284         return(bname);
285     } else {
286         do {
287             endp--;
288         } while (endp > path && *endp == '/');
289     }
290     
291     if (endp - path + 2 > sizeof(bname)) {
292         errno = ENAMETOOLONG;
293         return(NULL);
294     }
295     (void)strncpy(bname, path, endp - path + 1);
296     bname[endp - path + 1] = '\0';
297     return(bname);
298 }
299
300 char *
301 basename(path)
302 const char *path;
303 {
304     static char bname[MAXPATHLEN];
305     register const char *endp, *startp;
306     
307     /* Empty or NULL string gets treated as "." */
308     if (path == NULL || *path == '\0') {
309         (void)strcpy(bname, ".");
310         return(bname);
311     }
312     
313     /* Strip trailing slashes */
314     endp = path + strlen(path) - 1;
315     while (endp > path && *endp == '/')
316         endp--;
317     
318     /* All slashes becomes "/" */
319     if (endp == path && *endp == '/') {
320         (void)strcpy(bname, "/");
321         return(bname);
322     }
323     
324     /* Find the start of the base */
325     startp = endp;
326     while (startp > path && *(startp - 1) != '/')
327         startp--;
328     
329     if (endp - startp + 2 > sizeof(bname)) {
330         errno = ENAMETOOLONG;
331         return(NULL);
332     }
333     (void)strncpy(bname, startp, endp - startp + 1);
334     bname[endp - startp + 1] = '\0';
335     return(bname);
336 }
337