3 #include <sys/dirent.h>
12 #include <sys/sysctl.h>
13 #include <sys/utsname.h>
16 #include <nestedvm/socket.h>
18 int _syscall_set_errno(struct _reent *ptr, int err) {
23 extern int _stat_r(struct _reent *, const char *, struct stat *);
24 int _access_r(struct _reent *ptr, const char *pathname, int mode) {
26 if(_stat_r(ptr,pathname,&statbuf) < 0) return -1;
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;
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; }
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)
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)
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)
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)
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)
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)
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)
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)
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);
129 char *_getcwd_r(struct _reent *ptr, char *buf, size_t size) {
131 buf = __getcwd_r(ptr,buf,size);
132 return (long)buf == -1 ? NULL : buf;
138 char *ret = __getcwd_r(ptr,buf,size);
139 if((long)ret != -1) return ret;
142 if(ptr->_errno != ERANGE) return NULL;
146 pid_t _wait_r(struct _reent *ptr, int *status) {
147 return _waitpid_r(ptr,-1,status,0);
150 long _pathconf_r(struct _reent *ptr,const char *path, int name) {
153 fprintf(stderr,"WARNING: pathconf: Unknown \"name\": %d\n",name);
154 ptr->_errno = EINVAL;
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; }
166 fprintf(stderr,"WARNING: sysctl: Unknown name: %d\n",name[1]);
167 ptr->_errno = EINVAL;
181 char *ttyname(int fd) {
182 return isatty(fd) ? "/dev/console" : NULL;
185 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
189 if(act->sa_flags || act->sa_mask != ~((sigset_t)0)) { errno = EINVAL; return -1; }
190 old = signal(sig,act->sa_handler);
192 old = signal(sig,SIG_DFL);
196 oact->sa_handler = old;
198 oact->sa_mask = ~((sigset_t)0);
203 int sigfillset(sigset_t *set) {
204 *set = ~((sigset_t)0);
208 int sigemptyset(sigset_t *set) {
213 DIR *opendir(const char *path) {
218 fd = open(path,O_RDONLY);
219 if(fd < 0) return NULL;
221 if(fstat(fd,&sb) < 0 || !S_ISDIR(sb.st_mode)) {
227 dir = malloc(sizeof(*dir));
234 dir->dd_buf = malloc(sizeof(struct dirent));
235 dir->dd_size = sizeof(struct dirent);
236 if(dir->dd_buf == NULL) {
246 struct dirent *readdir(DIR *dir) {
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);
252 if(dir->dd_len <= 0) { dir->dd_len = 0; return NULL; }
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;
260 int closedir(DIR *dir) {
268 * Networking/Socket stuff
271 /* This should really be part of the newlib _reent structure */
274 char *inet_ntoa(struct in_addr in) {
276 const unsigned char *p = (void*) ∈
277 snprintf(buf,sizeof(buf),"%u.%u.%u.%u",p[0],p[1],p[2],p[3]);
281 struct servent *getservbyname(const char *name,const char *proto) {
285 static const char *h_errlist[] = { "No Error","Unknown host", "Host name lookup failure","Unknown server error","No address associated with name" };
287 const char *hstrerror(int err) {
288 if(err < 0 || err > 4) return "Unknown Error";
289 return h_errlist[err];
292 void herror(const char *string) {
293 fprintf(stderr,"%s: %s\n",string,hstrerror(h_errno));
296 extern int _resolve_hostname(const char *, char *buf, size_t *size);
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];
306 unsigned char buf[MAX_ADDRS*sizeof(struct in_addr)];
307 size_t size = sizeof(buf);
310 err = _resolve_hostname(hostname,buf,&size);
311 if(err != 0) { h_errno = err; return NULL; }
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];
316 strncpy(saved_hostname,hostname,sizeof(saved_hostname));
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;
329 * Other People's Code
332 /* FreeBSD's dirname/basename */
334 /* FIXME: Put these in a header */
337 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
338 * All rights reserved.
340 * Redistribution and use in source and binary forms, with or without
341 * modification, are permitted provided that the following conditions
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.
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.
367 static char bname[MAXPATHLEN];
368 register const char *endp;
370 /* Empty or NULL string gets treated as "." */
371 if (path == NULL || *path == '\0') {
372 (void)strcpy(bname, ".");
376 /* Strip trailing slashes */
377 endp = path + strlen(path) - 1;
378 while (endp > path && *endp == '/')
381 /* Find the start of the dir */
382 while (endp > path && *endp != '/')
385 /* Either the dir is "/" or there are no slashes */
387 (void)strcpy(bname, *endp == '/' ? "/" : ".");
392 } while (endp > path && *endp == '/');
395 if (endp - path + 2 > sizeof(bname)) {
396 errno = ENAMETOOLONG;
399 (void)strncpy(bname, path, endp - path + 1);
400 bname[endp - path + 1] = '\0';
408 static char bname[MAXPATHLEN];
409 register const char *endp, *startp;
411 /* Empty or NULL string gets treated as "." */
412 if (path == NULL || *path == '\0') {
413 (void)strcpy(bname, ".");
417 /* Strip trailing slashes */
418 endp = path + strlen(path) - 1;
419 while (endp > path && *endp == '/')
422 /* All slashes becomes "/" */
423 if (endp == path && *endp == '/') {
424 (void)strcpy(bname, "/");
428 /* Find the start of the base */
430 while (startp > path && *(startp - 1) != '/')
433 if (endp - startp + 2 > sizeof(bname)) {
434 errno = ENAMETOOLONG;
437 (void)strncpy(bname, startp, endp - startp + 1);
438 bname[endp - startp + 1] = '\0';
442 /* FreeBSD's uname */
445 struct utsname *name;
455 mib[1] = KERN_OSTYPE;
456 len = sizeof(name->sysname);
458 if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) {
464 name->sysname[sizeof(name->sysname) - 1] = '\0';
467 mib[1] = KERN_HOSTNAME;
468 len = sizeof(name->nodename);
470 if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) {
476 name->nodename[sizeof(name->nodename) - 1] = '\0';
479 mib[1] = KERN_OSRELEASE;
480 len = sizeof(name->release);
482 if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) {
488 name->release[sizeof(name->release) - 1] = '\0';
490 /* The version may have newlines in it, turn them into spaces. */
492 mib[1] = KERN_VERSION;
493 len = sizeof(name->version);
495 if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) {
501 name->version[sizeof(name->version) - 1] = '\0';
502 for (p = name->version; len--; ++p) {
503 if (*p == '\n' || *p == '\t') {
513 len = sizeof(name->machine);
515 if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) {
521 name->machine[sizeof(name->machine) - 1] = '\0';
525 /* FreeBSD's daemon() - modified for nestedvm */
527 daemon(nochdir, noclose)
528 int nochdir, noclose;
541 /*if (setsid() == -1)
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);