static void *Kmalloc(KonohaContext *kctx, size_t s, KTraceInfo *trace) { size_t *p = (size_t *)do_malloc(s #ifdef MEMORY_DEBUG + sizeof(size_t) #endif ); if(unlikely(p == NULL)) { KTraceApi(trace, SystemFault|UserFault, "malloc", LogUint("size", s), LogUint("UsedMemorySize", klib_malloced)); THROW_OutOfMemory(kctx, s); } #if GCDEBUG OLDTRACE_SWITCH_TO_KTrace(LOGPOL_DEBUG, LogText("@", "malloc"), KeyValue_p("from", p), KeyValue_p("to", ((char *)p)+s), LogUint("size", s)); #endif klib_malloced += s; #ifdef MEMORY_DEBUG p[0] = s; p += 1; #endif return (void *)p; }
//## boolean System.fchmod(int fd, int length) static KMETHOD System_fchmod(KonohaContext *kctx, KonohaStack *sfp) { int fd = sfp[1].intValue; int mode = sfp[2].intValue; int ret = fchmod(fd, mode); if(ret != 0) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace); KTraceErrorPoint(trace, fault, "fchmod", LogUint("fd", fd), LogUint("mode", mode) ); } KReturnUnboxValue(ret == 0); }
// NOTE: sys_flock can use for a file, only for //## @Native boolean System.flock(int fd, int opretaion); static KMETHOD System_flock(KonohaContext *kctx, KonohaStack *sfp) { int fd = sfp[1].intValue; int operation = sfp[2].intValue; int ret = flock(fd, operation); if(ret == -1) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace); KTraceErrorPoint(trace, fault, "flock", LogUint("fd", fd), LogUint("operation", operation) ); } KReturnUnboxValue(ret == 0); }
//## boolean System.ftruncate(int fd, int length) static KMETHOD System_ftruncate(KonohaContext *kctx, KonohaStack *sfp) { int fd = sfp[1].intValue; int length = sfp[2].intValue; int ret = ftruncate(fd, length); if(ret != 0) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace); KTraceErrorPoint(trace, fault, "ftruncate", LogUint("fd", fd), LogUint("length", length) ); } KReturnUnboxValue(ret == 0); }
//## boolean System.truncate(String path, int length) static KMETHOD System_truncate(KonohaContext *kctx, KonohaStack *sfp) { KMakeTrace(trace, sfp); char buffer[K_PATHMAX]; kString *path = sfp[1].asString; const char *systemPath = PLATAPI I18NModule.formatSystemPath(kctx, buffer, sizeof(buffer), kString_text(path), kString_size(path), trace); off_t length = (off_t)sfp[2].intValue; int ret = truncate(systemPath, length); if(ret == -1) { int fault = KLIB DiagnosisFaultType(kctx, kString_GuessUserFault(path)|SystemError, trace); KTraceErrorPoint(trace, fault, "truncate", LogFileName(kString_text(path)), LogUint("length", length), LogErrno); } else { KTraceChangeSystemPoint(trace, "truncate", LogFileName(kString_text(path)), LogUint("length", length)); } KReturnUnboxValue(ret != -1); }
//## @Native int System.lseek(int fd, int offset, int whence) static KMETHOD System_lseek(KonohaContext *kctx, KonohaStack *sfp) { int fd = sfp[1].intValue; int offset = sfp[2].intValue; int whence = sfp[3].intValue; off_t ret_offset = lseek(fd, offset, whence); if(ret_offset == -1) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace); KTraceErrorPoint(trace, fault, "lseek", LogUint("offset", offset), LogUint("whence", whence) ); } KReturnUnboxValue((int)ret_offset); }
static KMETHOD System_fchown(KonohaContext *kctx, KonohaStack *sfp) { int fd = sfp[1].intValue; uid_t owner = sfp[2].intValue; gid_t group = sfp[3].intValue; int ret = fchown(fd, owner, group); if(ret == -1) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace); KTraceErrorPoint(trace, fault, "fchown", LogUint("fd", fd), LogUint("owner", owner), LogUint("group", group) ); } KReturnUnboxValue(ret == 0); }
//## int System.open(String pathname, int flags, int mode) static KMETHOD System_open_mode(KonohaContext *kctx, KonohaStack *sfp) { kString *s = sfp[1].asString; const char *pathname = S_text(s); int flags = sfp[2].intValue; mode_t mode = sfp[3].intValue; int ret = open(pathname, flags, mode); if(ret == -1) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, kString_guessUserFault(s)|SystemError, trace); KTraceErrorPoint(trace, fault, "open_mode", LogText("pathname", pathname), LogUint("flags", flags), LogUint("mode", mode) ); } KReturnUnboxValue(ret); }
//## int System.fchdir(int fd) static KMETHOD System_fchdir(KonohaContext *kctx, KonohaStack *sfp) { int fd = fchdir(sfp[1].intValue); if(fd == -1) { // TODO: throw KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace); KTraceErrorPoint(trace, fault, "fchdir", LogUint("fd", fd)); } KReturnUnboxValue(fd == 0); }
//## int System.sendto(int socket, Bytes message, int flags, String dstIP, int dstPort, int family); static KMETHOD System_sendto(KonohaContext *kctx, KonohaStack* sfp) { kBytes *ba = sfp[2].asBytes; struct sockaddr_in addr; kString* s = sfp[4].asString; toSockaddr(&addr, (char *)S_text(s), WORD2INT(sfp[5].intValue), WORD2INT(sfp[6].intValue)); // Broken Pipe Signal Mask #if defined(__linux__) __sighandler_t oldset = signal(SIGPIPE, SIG_IGN); __sighandler_t ret_signal = SIG_ERR; #elif defined(__APPLE__) || defined(__NetBSD__) sig_t oldset = signal(SIGPIPE, SIG_IGN); sig_t ret_signal = SIG_ERR; #endif int ret = sendto( WORD2INT(sfp[1].intValue), ba->buf, ba->bytesize, (int)sfp[3].intValue, (struct sockaddr *)&addr, sizeof(struct sockaddr) ); if(ret < 0) { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "sendto"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } if(oldset != SIG_ERR) { ret_signal = signal(SIGPIPE, oldset); if(ret_signal == SIG_ERR) { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "signal"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } } KReturnUnboxValue(ret); }
//## int System.shutdown(int socket, int how); KMETHOD System_shutdown(KonohaContext *kctx, KonohaStack* sfp) { int ret = shutdown(WORD2INT(sfp[1].intValue), WORD2INT(sfp[2].intValue)); if(ret != 0) { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "shutdown"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } KReturnUnboxValue(ret); }
static void logpool_test_Write(logpool_t *logpool) { long i = n; const char *s = "Good Bye"; void *args; logpool_record(logpool, &args, LOG_NOTICE, "check", LogText("string", s), LogUint("tid", i%10), LOG_END ); n++; }
//## String System.ttyname(int fd); static KMETHOD System_ttyname(KonohaContext *kctx, KonohaStack *sfp) { int fd = sfp[1].intValue; char buf[K_PAGESIZE]; int ret = ttyname_r(fd, buf, sizeof(buf)); if(ret != 0) { KMakeTrace(trace, sfp); int fault = KLIB DiagnosisFaultType(kctx, SystemError, trace); KTraceErrorPoint(trace, fault, "ttyname", LogUint("fd", fd), LogErrno); } KReturn(KLIB new_kString(kctx, OnStack, buf, strlen(buf), 0)); }
//## int System.sockatmark(int socket); KMETHOD System_sockatmark(KonohaContext *kctx, KonohaStack* sfp) { int ret = sockatmark(WORD2INT(sfp[1].intValue)); if(ret < 0) { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "sockadmark"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } KReturnUnboxValue(ret); }
static kObjectVar *KallocObject(KonohaContext *kctx, size_t size, KTraceInfo *trace) { GcManager *mng = (GcManager *)kctx->gcContext; int page_size = (size / sizeof(kGCObject0)) >> 1; DBG_ASSERT(page_size <= 4); kGCObject *o = NULL; FREELIST_POP(o,page_size); MSGC(page_size).freelist.size -= 1; do_bzero((void *)o, size); #if GCDEBUG OLDTRACE_SWITCH_TO_KTrace(LOGPOL_DEBUG, LogText("@", "new"), KeyValue_p("ptr", o), LogUint("size", size)); #endif return (kObjectVar *)o; }
//## int System.setsockopt(int socket, int option, int value); KMETHOD System_setsockopt(KonohaContext *kctx, KonohaStack* sfp) { int ret = setsockopt( WORD2INT(sfp[1].intValue), SOL_SOCKET, (int)sfp[2].intValue, &sfp[3].intValue, sizeof(sfp[3].intValue) ); if(ret != 0) { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "setsockopt"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } KReturnUnboxValue(ret); }
static void Kfree(KonohaContext *kctx, void *p, size_t s) { size_t *pp = (size_t *)p; klib_malloced -= s; #ifdef MEMORY_DEBUG DBG_ASSERT(pp[-1] == s); pp -= 1; #endif #if GCDEBUG OLDTRACE_SWITCH_TO_KTrace(LOGPOL_DEBUG, LogText("@", "free"), KeyValue_p("from", p), KeyValue_p("to", ((char *)p)+s), LogUint("size", s)); #endif do_Free(pp, s #ifdef MEMORY_DEBUG + sizeof(size_t) #endif ); }
//## int System.bind(int socket, String srcIP, int srcPort, int family); KMETHOD System_bind(KonohaContext *kctx, KonohaStack* sfp) { struct sockaddr_in addr; toSockaddr(&addr, S_text(sfp[2].asString), WORD2INT(sfp[3].intValue), WORD2INT(sfp[4].intValue) ); int ret = bind(WORD2INT(sfp[1].intValue), (struct sockaddr *)&addr, sizeof(addr) ); if(ret != 0) { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "bind"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } KReturnUnboxValue(ret); }
//## int System.accept(int socket, SockAddr remoteInfo); KMETHOD System_accept(KonohaContext *kctx, KonohaStack* sfp) { struct _kSockAddr *sa = (struct _kSockAddr *)sfp[2].asObject; struct sockaddr_in *addr = sa->sockaddr_in; int addrLen = sizeof(struct sockaddr_in); int ret = accept( WORD2INT(sfp[1].intValue), (struct sockaddr *)addr, (socklen_t *)&addrLen ); if(ret >= 0) { // fromSockaddr(kctx, sa, addr); } else { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "accept"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } KReturnUnboxValue(ret); }
//## int System.socketpair(int family, int type, int protocol, int[] pairCSock); static KMETHOD System_socketpair(KonohaContext *kctx, KonohaStack* sfp) { int ret = -2; kArray *a = sfp[4].asArray; if(kArray_size(a)) { int pairFd[2]; if((ret = socketpair(WORD2INT(sfp[1].intValue), WORD2INT(sfp[2].intValue), WORD2INT(sfp[3].intValue), pairFd)) == 0) { a->kintItems[0] = pairFd[0]; a->kintItems[1] = pairFd[1]; } else { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "socketpair"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } } KReturnUnboxValue(ret); }
//## int System.getsockopt(int socket, int option); KMETHOD System_getsockopt(KonohaContext *kctx, KonohaStack* sfp) { int val; int valLen = sizeof(val); int ret = getsockopt( WORD2INT(sfp[1].intValue), SOL_SOCKET, (int)sfp[2].intValue, &val, (socklen_t *)&valLen ); if(ret == 0) { ret = val; } else { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "getsockopt"), LogUint("errno", errno), LogText("errstr", strerror(errno)) ); } KReturnUnboxValue(ret); }