cleaner gcc build process
[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 #include <sys/sysctl.h>
13 #include <sys/utsname.h>
14 #include <paths.h>
15
16 #include <nestedvm/socket.h>
17
18 int _syscall_set_errno(struct _reent *ptr, int err) {
19     ptr->_errno = -err;
20     return -1;
21 }
22
23 extern int _stat_r(struct _reent *, const char *, struct stat *);
24 int _access_r(struct _reent *ptr, const char *pathname, int mode) {
25     struct stat statbuf;
26     if(_stat_r(ptr,pathname,&statbuf) < 0) return -1;
27     return 0;
28 }
29
30 /* NestedVM doesn't, and probably never will, support this security related stuff */
31 uid_t getuid() { return 0; }
32 gid_t getgid() { return 0; }
33 uid_t geteuid() { return 0; }
34 gid_t getegid() { return 0; }
35 int getgroups(int gidsetlen, gid_t *gidset) {
36     if(gidsetlen) *gidset = 0;
37     return 1;
38 }
39 mode_t umask(mode_t new) { return 0022; }
40 int _chmod_r(struct _reent *ptr, const char *f, mode_t mode) { return 0; }
41 int _fchmod_r(struct _reent *ptr, int fd, mode_t mode) { return 0; }
42 int _chown_r(struct _reent *ptr, const char *f, uid_t uid, gid_t gid) { return 0; }
43 int _fchown_r(struct _reent *ptr, int fd, uid_t uid, gid_t gid) { return 0; }
44
45 #define REENT_WRAPPER0R(f,rt) \
46     extern rt _##f##_r(struct _reent *ptr); \
47     rt f() { return _##f##_r(_REENT); }
48 #define REENT_WRAPPER0(f) REENT_WRAPPER0R(f,int)
49
50 #define REENT_WRAPPER1R(f,rt,t1) \
51     extern rt _##f##_r(struct _reent *ptr, t1 a); \
52     rt f(t1 a) { return _##f##_r(_REENT,a); }
53 #define REENT_WRAPPER1(f,t1) REENT_WRAPPER1R(f,int,t1)
54
55 #define REENT_WRAPPER2R(f,rt,t1,t2) \
56     extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b); \
57     rt f(t1 a, t2 b) { return _##f##_r(_REENT,a,b); }
58 #define REENT_WRAPPER2(f,t1,t2) REENT_WRAPPER2R(f,int,t1,t2)
59
60 #define REENT_WRAPPER3R(f,rt,t1,t2,t3) \
61     extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c); \
62     rt f(t1 a, t2 b, t3 c) { return _##f##_r(_REENT,a,b,c); }
63 #define REENT_WRAPPER3(f,t1,t2,t3) REENT_WRAPPER3R(f,int,t1,t2,t3)
64
65 #define REENT_WRAPPER4R(f,rt,t1,t2,t3,t4) \
66 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d); \
67 rt f(t1 a, t2 b, t3 c, t4 d) { return _##f##_r(_REENT,a,b,c,d); }
68 #define REENT_WRAPPER4(f,t1,t2,t3,t4) REENT_WRAPPER4R(f,int,t1,t2,t3,t4)
69
70 #define REENT_WRAPPER5R(f,rt,t1,t2,t3,t4,t5) \
71 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d, t5 e); \
72 rt f(t1 a, t2 b, t3 c, t4 d, t5 e) { return _##f##_r(_REENT,a,b,c,d,e); }
73 #define REENT_WRAPPER5(f,t1,t2,t3,t4,t5) REENT_WRAPPER5R(f,int,t1,t2,t3,t4,t5)
74
75 #define REENT_WRAPPER6R(f,rt,t1,t2,t3,t4,t5,t6) \
76 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d, t5 e, t6 f); \
77 rt f(t1 a, t2 b, t3 c, t4 d, t5 e, t6 f) { return _##f##_r(_REENT,a,b,c,d,e,f); }
78 #define REENT_WRAPPER6(f,t1,t2,t3,t4,t5,t6) REENT_WRAPPER6R(f,int,t1,t2,t3,t4,t5,t6)
79
80 REENT_WRAPPER2(mkdir,const char *,mode_t)
81 REENT_WRAPPER2(access,const char *,int)
82 REENT_WRAPPER1(rmdir,const char *)
83 REENT_WRAPPER1R(sysconf,long,int)
84 REENT_WRAPPER1(chdir,const char*)
85 REENT_WRAPPER2(utime,const char *,const struct utimbuf *)
86 REENT_WRAPPER1(pipe,int *)
87 REENT_WRAPPER2(dup2,int,int)
88 REENT_WRAPPER3(waitpid,pid_t,int *,int)
89 REENT_WRAPPER2R(getcwd,char *,char *,size_t)
90 REENT_WRAPPER2R(_getcwd,char *,char *,size_t)
91 REENT_WRAPPER2(symlink,const char *,const char *)
92 REENT_WRAPPER3(readlink,const char *, char *,int)
93 REENT_WRAPPER3(chown,const char *,uid_t,gid_t)
94 REENT_WRAPPER3(fchown,int,uid_t,gid_t)
95 REENT_WRAPPER3(lchown,const char *,uid_t,gid_t)
96 REENT_WRAPPER2(chmod,const char *,mode_t)
97 REENT_WRAPPER2(fchmod,int,mode_t)
98 REENT_WRAPPER2(lstat,const char *,struct stat *)
99 REENT_WRAPPER4(getdents,int, char *, size_t,long *)
100 REENT_WRAPPER1(dup,int)
101 REENT_WRAPPER2R(pathconf,long,const char *,int)
102 REENT_WRAPPER0(vfork)
103 REENT_WRAPPER1(chroot,const char *)
104 REENT_WRAPPER3(mknod,const char *,mode_t,dev_t)
105 REENT_WRAPPER2(ftruncate,int,off_t)
106 REENT_WRAPPER1(usleep,unsigned int)
107 REENT_WRAPPER2(mkfifo,const char *, mode_t)
108 REENT_WRAPPER3(klogctl,int,char*,int)
109 REENT_WRAPPER2R(realpath,char *,const char *,char *)
110 REENT_WRAPPER6(_sysctl,int *,int, void *, size_t*, void *, size_t)
111 REENT_WRAPPER6(sysctl,int *, int, void *, size_t*, void *, size_t)
112 REENT_WRAPPER2(getpriority,int,int)
113 REENT_WRAPPER3(setpriority,int,int,int)
114 REENT_WRAPPER3(connect,int,const struct sockaddr *,socklen_t)
115 REENT_WRAPPER3(socket,int,int,int)
116 REENT_WRAPPER3(_resolve_hostname,const char *,char*,size_t*)
117 REENT_WRAPPER3(accept,int,struct sockaddr *,socklen_t*)
118 REENT_WRAPPER5(getsockopt,int,int,int,void*,socklen_t*)
119 REENT_WRAPPER5(setsockopt,int,int,int,const void*,socklen_t)
120 REENT_WRAPPER3(bind,int,const struct sockaddr *,socklen_t)
121 REENT_WRAPPER2(listen,int,int)
122 REENT_WRAPPER2(shutdown,int,int)
123
124 extern int __execve_r(struct _reent *ptr, const char *path, char *const argv[], char *const envp[]);
125 int _execve(const char *path, char *const argv[], char *const envp[]) {
126     return __execve_r(_REENT,path,argv,envp);
127 }
128
129 char *_getcwd_r(struct _reent *ptr, char *buf, size_t size) {
130     if(buf != NULL) {
131         buf = __getcwd_r(ptr,buf,size);
132         return (long)buf == -1 ? NULL : buf;
133     }
134     
135     size = 256;
136     for(;;) {
137         buf = malloc(size);
138         char *ret = __getcwd_r(ptr,buf,size);
139         if((long)ret != -1) return ret;
140         free(buf);
141         size *= 2;
142         if(ptr->_errno != ERANGE) return NULL;
143     }
144 }
145
146 pid_t _wait_r(struct _reent *ptr, int *status) {
147     return _waitpid_r(ptr,-1,status,0);
148 }
149
150 long _pathconf_r(struct _reent *ptr,const char *path, int name) {
151     switch(name) {
152         default:
153             fprintf(stderr,"WARNING: pathconf: Unknown \"name\": %d\n",name);
154             ptr->_errno = EINVAL;
155             return -1;
156     }
157 }
158
159 int _sysctl_r(struct _reent *ptr, int *name, int namelen, void *oldp, size_t *oldlen, void *newp, size_t newlen) {
160     if(name[0] != CTL_USER) return _sysctl(name,namelen,oldp,oldlen,newp,newlen);
161     if(newp != NULL) { ptr->_errno = EPERM; return -1; }
162     if(namelen != 2) { ptr->_errno = EINVAL; return -1; }
163     
164     switch(name[1]) {
165         default:
166             fprintf(stderr,"WARNING: sysctl: Unknown name: %d\n",name[1]);
167             ptr->_errno = EINVAL;
168             return -1;
169     }
170 }
171
172 void sync() {
173     /* do nothing*/
174 }
175
176 int fsync(int fd) {
177     /* do nothing */
178     return 0;
179 }
180
181 char *ttyname(int fd) {
182     return isatty(fd) ? "/dev/console" : NULL;
183 }
184
185 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
186     _sig_func_ptr old;
187     _sig_func_ptr new;
188     if(act) {
189         if(act->sa_flags || act->sa_mask != ~((sigset_t)0)) { errno = EINVAL; return -1; }
190         old = signal(sig,act->sa_handler);
191     } else if(oact) {
192         old = signal(sig,SIG_DFL);
193         signal(sig,old);
194     }
195     if(oact) {
196         oact->sa_handler = old;
197         oact->sa_mask = 0;
198         oact->sa_mask = ~((sigset_t)0);
199     }
200     return 0;
201 }
202
203 int sigfillset(sigset_t *set) {
204     *set = ~((sigset_t)0);
205     return 0;
206 }
207
208 int sigemptyset(sigset_t *set) {
209     *set = (sigset_t) 0;
210     return 0;
211 }
212
213 DIR *opendir(const char *path) {
214     struct stat sb;
215     int fd;
216     DIR *dir;
217     
218     fd = open(path,O_RDONLY);
219     if(fd < 0) return NULL;
220     
221     if(fstat(fd,&sb) < 0 || !S_ISDIR(sb.st_mode)) {
222         close(fd);
223         errno = ENOTDIR;
224         return NULL;
225     }
226     
227     dir = malloc(sizeof(*dir));
228     if(dir == NULL) {
229         close(fd);
230         errno = ENOMEM;
231         return NULL;
232     }
233     dir->dd_fd = fd;
234     dir->dd_buf = malloc(sizeof(struct dirent));
235     dir->dd_size = sizeof(struct dirent);
236     if(dir->dd_buf == NULL) {
237         close(fd);
238         free(dir);
239         return NULL;
240     }
241     dir->dd_loc = 0;
242     dir->dd_len = 0;
243     return dir;
244 }
245
246 struct dirent *readdir(DIR *dir) {
247     struct dirent *dp;
248     errno = 0;
249     if(dir->dd_loc == 0 || dir->dd_loc == dir->dd_len) {
250         dir->dd_len = getdents(dir->dd_fd,dir->dd_buf,dir->dd_size,NULL);
251         dir->dd_loc = 0;
252         if(dir->dd_len <= 0) { dir->dd_len = 0; return NULL; }
253     }
254     dp = (struct dirent*) (dir->dd_buf + dir->dd_loc);
255     if(dp->d_reclen == 0 || dp->d_reclen > dir->dd_len - dir->dd_loc) return NULL;
256     dir->dd_loc += dp->d_reclen;
257     return dp;
258 }
259
260 int closedir(DIR *dir) {
261     int fd = dir->dd_fd;
262     free(dir->dd_buf);
263     free(dir);
264     return close(fd);
265 }
266
267 /*
268  * Networking/Socket stuff
269  */
270
271 /* This should really be part of the newlib _reent structure */
272 int h_errno;
273
274 char *inet_ntoa(struct in_addr in) {
275     static char buf[18];
276     const unsigned char *p = (void*) &in;
277     snprintf(buf,sizeof(buf),"%u.%u.%u.%u",p[0],p[1],p[2],p[3]);
278     return buf;
279 }
280
281 struct servent *getservbyname(const char *name,const char *proto) {
282     return NULL;
283 }
284
285 static const char *h_errlist[] = { "No Error","Unknown host", "Host name lookup failure","Unknown server error","No address associated with name" };
286
287 const char *hstrerror(int err) {
288     if(err < 0 || err > 4) return "Unknown Error";
289     return h_errlist[err];
290 }
291
292 void herror(const char *string) {
293     fprintf(stderr,"%s: %s\n",string,hstrerror(h_errno));
294 }
295
296 extern int _resolve_hostname(const char *, char *buf, size_t *size);
297
298 struct hostent *gethostbyname(const char *hostname) {
299 #define MAX_ADDRS 256
300     static struct hostent hostent;
301     static char saved_hostname[128];
302     static char *addr_list[MAX_ADDRS+1];
303     static char addr_list_buf[MAX_ADDRS*sizeof(struct in_addr)];
304     static char *aliases[1];
305     
306     unsigned char buf[MAX_ADDRS*sizeof(struct in_addr)];
307     size_t size = sizeof(buf);
308     int err,i,n=0;
309     
310     err = _resolve_hostname(hostname,buf,&size);
311     if(err != 0) { h_errno = err; return NULL; }
312     
313     memcpy(addr_list_buf,buf,size);
314     for(i=0;i<size;i += sizeof(struct in_addr)) addr_list[n++] = &addr_list_buf[i];
315     addr_list[n] = NULL;
316     strncpy(saved_hostname,hostname,sizeof(saved_hostname));
317     aliases[0] = NULL;
318     
319     hostent.h_name = saved_hostname;
320     hostent.h_aliases = aliases;
321     hostent.h_addrtype = AF_INET;
322     hostent.h_length = sizeof(struct in_addr);
323     hostent.h_addr_list = addr_list;
324     
325     return &hostent;
326 }
327
328 /*
329  * Other People's Code 
330  */
331
332 /* FreeBSD's dirname/basename */
333
334 /* FIXME: Put these in a header */
335
336 /*
337  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
338  * All rights reserved.
339  *
340  * Redistribution and use in source and binary forms, with or without
341  * modification, are permitted provided that the following conditions
342  * are met:
343  * 1. Redistributions of source code must retain the above copyright
344  *    notice, this list of conditions and the following disclaimer.
345  * 2. Redistributions in binary form must reproduce the above copyright
346  *    notice, this list of conditions and the following disclaimer in the
347  *    documentation and/or other materials provided with the distribution.
348  * 3. The name of the author may not be used to endorse or promote products
349  *    derived from this software without specific prior written permission.
350  *
351  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
352  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
353  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
354  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
355  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
356  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
357  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
358  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
359  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
360  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
361  */
362
363 char *
364 dirname(path)
365 const char *path;
366 {
367     static char bname[MAXPATHLEN];
368     register const char *endp;
369     
370     /* Empty or NULL string gets treated as "." */
371     if (path == NULL || *path == '\0') {
372         (void)strcpy(bname, ".");
373         return(bname);
374     }
375     
376     /* Strip trailing slashes */
377     endp = path + strlen(path) - 1;
378     while (endp > path && *endp == '/')
379         endp--;
380     
381     /* Find the start of the dir */
382     while (endp > path && *endp != '/')
383         endp--;
384     
385     /* Either the dir is "/" or there are no slashes */
386     if (endp == path) {
387         (void)strcpy(bname, *endp == '/' ? "/" : ".");
388         return(bname);
389     } else {
390         do {
391             endp--;
392         } while (endp > path && *endp == '/');
393     }
394     
395     if (endp - path + 2 > sizeof(bname)) {
396         errno = ENAMETOOLONG;
397         return(NULL);
398     }
399     (void)strncpy(bname, path, endp - path + 1);
400     bname[endp - path + 1] = '\0';
401     return(bname);
402 }
403
404 char *
405 basename(path)
406 const char *path;
407 {
408     static char bname[MAXPATHLEN];
409     register const char *endp, *startp;
410     
411     /* Empty or NULL string gets treated as "." */
412     if (path == NULL || *path == '\0') {
413         (void)strcpy(bname, ".");
414         return(bname);
415     }
416     
417     /* Strip trailing slashes */
418     endp = path + strlen(path) - 1;
419     while (endp > path && *endp == '/')
420         endp--;
421     
422     /* All slashes becomes "/" */
423     if (endp == path && *endp == '/') {
424         (void)strcpy(bname, "/");
425         return(bname);
426     }
427     
428     /* Find the start of the base */
429     startp = endp;
430     while (startp > path && *(startp - 1) != '/')
431         startp--;
432     
433     if (endp - startp + 2 > sizeof(bname)) {
434         errno = ENAMETOOLONG;
435         return(NULL);
436     }
437     (void)strncpy(bname, startp, endp - startp + 1);
438     bname[endp - startp + 1] = '\0';
439     return(bname);
440 }
441
442 /* FreeBSD's uname */
443 int
444 uname(name)
445 struct utsname *name;
446 {
447         int mib[2], rval;
448         size_t len;
449         char *p;
450         int oerrno;
451     
452         rval = 0;
453     
454         mib[0] = CTL_KERN;
455         mib[1] = KERN_OSTYPE;
456         len = sizeof(name->sysname);
457         oerrno = errno;
458         if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) {
459                 if(errno == ENOMEM)
460                         errno = oerrno;
461                 else
462                         rval = -1;
463         }
464         name->sysname[sizeof(name->sysname) - 1] = '\0';
465     
466         mib[0] = CTL_KERN;
467         mib[1] = KERN_HOSTNAME;
468         len = sizeof(name->nodename);
469         oerrno = errno;
470         if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) {
471                 if(errno == ENOMEM)
472                         errno = oerrno;
473                 else
474                         rval = -1;
475         }
476         name->nodename[sizeof(name->nodename) - 1] = '\0';
477     
478         mib[0] = CTL_KERN;
479         mib[1] = KERN_OSRELEASE;
480         len = sizeof(name->release);
481         oerrno = errno;
482         if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) {
483                 if(errno == ENOMEM)
484                         errno = oerrno;
485                 else
486                         rval = -1;
487         }
488         name->release[sizeof(name->release) - 1] = '\0';
489     
490         /* The version may have newlines in it, turn them into spaces. */
491         mib[0] = CTL_KERN;
492         mib[1] = KERN_VERSION;
493         len = sizeof(name->version);
494         oerrno = errno;
495         if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) {
496                 if (errno == ENOMEM)
497                         errno = oerrno;
498                 else
499                         rval = -1;
500         }
501         name->version[sizeof(name->version) - 1] = '\0';
502         for (p = name->version; len--; ++p) {
503                 if (*p == '\n' || *p == '\t') {
504                         if (len > 1)
505                                 *p = ' ';
506                         else
507                                 *p = '\0';
508                 }
509         }
510     
511         mib[0] = CTL_HW;
512         mib[1] = HW_MACHINE;
513         len = sizeof(name->machine);
514         oerrno = errno;
515         if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) {
516                 if (errno == ENOMEM)
517                         errno = oerrno;
518                 else
519                         rval = -1;
520         }
521         name->machine[sizeof(name->machine) - 1] = '\0';
522         return (rval);
523 }
524
525 /* FreeBSD's daemon() - modified for nestedvm */
526 int
527 daemon(nochdir, noclose)
528 int nochdir, noclose;
529 {
530         int fd;
531     
532         switch (fork()) {
533         case -1:
534             return (-1);
535         case 0:
536             break;
537         default:
538             _exit(0);
539         }
540     
541         /*if (setsid() == -1)
542                 return (-1);*/
543     
544         if (!nochdir)
545                 (void)chdir("/");
546     
547         if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
548                 (void)dup2(fd, STDIN_FILENO);
549                 (void)dup2(fd, STDOUT_FILENO);
550                 (void)dup2(fd, STDERR_FILENO);
551                 if (fd > 2)
552                         (void)close(fd);
553         }
554         return (0);
555 }