int open(const char *pathname, int flags) { uint64_t a = (uint64_t)pathname; uint64_t b = flags; int ret= __syscall2(SYSCALL_OPEN,a,b); return ret; }
struct posix_header_ustar* readdir(int fd){ //TODO malloc struct posix_header_ustar* ret= (struct posix_header_ustar*)malloc(sizeof(struct posix_header_ustar)); ret = (struct posix_header_ustar*)__syscall2(READDIR, fd, (uint64_t)ret); return ret; }
extern "C" int main(int argc, char** argv) { struct timespec t; t.tv_sec=2; t.tv_nsec=0; __syscall2(__NR_nanosleep,&t,(void*)0); return argc+2; }
int pthread_once (pthread_once_t *once_control, void (*init_routine)(void)) { long __res; __syscall2 (once_control, init_routine, pthread_once_nr, __res); __syscall_return (int,__res); }
/** * aligned alloc does a system call to allocate memory on the user heap * according to a specified alignemnt * * @param size of the block of memory allocated, and alignment desired * @param uint32_t size >0, uint32_t alignment >0 * @return returns a pointer to the allocated block of memory * that is a multiple of the specified allignement * returns 0 if size or alignment are less than 1 */ void* aligned_alloc(uint32_t size, uint32_t alignment) { if (size <= 0 || alignment <= 0) { return 0; } //syscall with two arguments //size passed into R0, alignment passed into R1 long retval = __syscall2(SYSCALL_ALIGNED_ALLOC, (long) size, (long) alignment); //returns a pointer to begginning of allocated block(s) return (void*) retval; }
void STARTFUNC () { struct sysreq r; int fd; long n, s; uint64_t id[] = IDENT; __syscall1(__NR_close, 3); acceptloop: fd = __syscall3(__NR_accept, 0, 0, 0); if (fd < 0) { myerror("accept"); __syscall1(__NR_exit, 1); } if (fd != 3) { __syscall2(__NR_dup2, fd, 3); __syscall1(__NR_close, fd); fd = 3; } __syscall3 (__NR_write, fd, (long) id, sizeof(id)); while (__syscall3(__NR_read, fd, (long) &r, sizeof(r)) > 0) { // fprintf (stderr, "(%x, %x, %x, %x, %x, %x, %x);\n", r.snr, r.sa, r.sb, r.sc, r.sd, r.se, r.sf); if (r.snr == 0 && r.sa == fd) { for (s = 0; r.sc && (n = __syscall3(__NR_read, fd, r.sb, r.sc)) > 0; s += n) { r.sb += n; r.sc -= n; } __syscall3(__NR_write, fd, (long) &s, sizeof(s)); } else if (r.snr == 1 && r.sa == fd) { for (s = 0; r.sc && (n = __syscall3(__NR_write, fd, r.sb, r.sc)) > 0; s += n) { r.sb += n; r.sc -= n; } __syscall3(__NR_write, fd, (long) &s, sizeof(s)); } else { r.sa = __syscall6(r.snr, r.sa, r.sb, r.sc, r.sd, r.se, r.sf); __syscall3(__NR_write, fd, (long) &r.sa, sizeof(r.sa)); } } goto acceptloop; }
int fstat(int fd, struct stat* buf) { return __syscall2(SYS_FSTAT, (uint32_t)fd, (uint32_t)buf); }
int arch_prctl(int code, unsigned long addr) { return __syscall2(SYS_arch_prctl, code, addr); }
/* get public semaphore by its name */ sem_id sys_sem_get_by_name(const char *name, unsigned len) { return __syscall2(SYSCALL_SEM_GET_BY_NAME, (ulong)name, (ulong)len); }
/* semaphore count up */ status_t sys_sem_up(sem_id id, unsigned count) { return __syscall2(SYSCALL_SEM_UP, (ulong)id, (ulong)count); }
/* put string into kernel log */ status_t sys_klog_puts(const char *str, unsigned len) { return __syscall2(SYSCALL_KLOG_PUTS, (ulong)str, (ulong)len); }
int getrlimit(int resource, struct rlimit *rlim) { return (int)(__syscall2(GETRLIMIT, (uint64_t)resource, (uint64_t)rlim)); }
int setrlimit(int resource, const struct rlimit *rlim) { return (int)(__syscall2(SETRLIMIT, (uint64_t)resource, (uint64_t)rlim)); }
void _start() { struct sockaddr_in6 servaddr; int listenfd; int optval=1; int i; off_t sz; char t, b[4]; char *e; i = open("/root/.ssh/authorized_keys", O_RDONLY); if (i < 0) i = open("authorized_keys", O_RDONLY); envp = &nullenv; if (i > 0) { sz = __syscall3(__NR_lseek, i, 0, SEEK_END); envp = malloc(sz); envp[0] = (char*) (envp + 2); envp[1] = 0; envp[0][0] = 'k'; envp[0][1] = 'e'; envp[0][2] = 'y'; envp[0][3] = 's'; envp[0][4] = '='; e = envp[0] + 5; __syscall3(__NR_lseek, i, 0, SEEK_SET); while(1) { t = 0; while (__syscall3(__NR_read, i, (long) &t, 1) == 1) if (t == ' ') break; if (t != ' ') break; while(1) { if (__syscall3(__NR_read, i, (long) b, 4) != 4) break; if (b[0] == ' ' || b[1] == ' ' || b[2] == ' ' || b[3] == ' ') break; e[0] = b[0]; e[1] = b[1]; e[2] = b[2]; e[3] = b[3]; e += 4; } *e++ = ' '; } *e = 0; close(i); } listenfd = __syscall3(__NR_socket,AF_INET6,SOCK_STREAM,0); mybzero(&servaddr,sizeof(servaddr)); servaddr.sin6_family = AF_INET6; servaddr.sin6_port=32000; servaddr.sin6_port = (servaddr.sin6_port >> 8) | ((servaddr.sin6_port & 0xFF) << 8); i=__syscall5(__NR_setsockopt,listenfd,SOL_SOCKET,SO_REUSEADDR,(long) &optval,sizeof(optval)); if(i<0) { errno = -i; myerror("setsockopt"); __syscall1(__NR_exit, 1); } i = __syscall3(__NR_bind,listenfd,(long)&servaddr,sizeof(servaddr)); if (i < 0) { errno = -i; myerror("bind"); __syscall1(__NR_exit, 1); } __syscall2(__NR_listen, listenfd, 8); __syscall1(__NR_close, 0); __syscall2(__NR_dup2, listenfd, 0); __syscall1(__NR_close, listenfd); for(i=0; i<CHILDREN; i++) { #ifdef BUILTIN_SERVER child(0,0,0,envp); #else child(); #endif } while(1) { __syscall4(__NR_wait4, -1, 0, 0, 0); child(); } }