/* -- in out/nil error */ static int ex_pipe(lua_State *L) { int fd[2]; if (-1 == pipe(fd)) return push_error(L); closeonexec(fd[0]); closeonexec(fd[1]); new_file(L, fd[0], "r"); new_file(L, fd[1], "w"); return 2; }
P addsocket_dup(P fd) { P retc = OK; struct socketstore* next; struct SocketType* type = NULL; for (next = socketstore_head; next; next = next->next) if (next->fd == fd) { type = &next->type; break; } if (! type) { retc = FD_NOTFOUND; goto ret; } if (type->listener) { retc = FD_ISSOCKET; goto ret; } if (! type->fork) { if ((retc = nocloseonexec(fd))) goto ret; } else if ((retc = closeonexec(fd))) goto ret; sockprintdebug("dup", next); ret: if (retc) delsocket_force(fd); return retc; }
void runtime·netpollinit(void) { kq = runtime·kqueue(); if(kq < 0) { runtime·printf("netpollinit: kqueue failed with %d\n", -kq); runtime·throw("netpollinit: kqueue failed"); } runtime·closeonexec(kq); }
// -------- addsocket ----------------------------- // After opening a socket, call addsocket to increase maxsocket, // care for recsocket, and add socket to sock_fds. P addsocket(P fd, const struct SocketType* type, const union SocketInfo* info) { P retc = OK; struct socketstore* next; for (next = socketstore_head; next; next = next->next) if (next->fd == fd) { if (type->listener || next->type.listener) return SOCK_STATE; return OK; } if (! (next = (struct socketstore*) malloc(sizeof(struct socketstore)))) dm_error(errno, "Malloc failure creating socketstore"); next->fd = fd; next->type = *type; if (info) next->info = *info; next->pid = getpid(); next->redirector = -1; next->next = NULL; next->last = socketstore_tail; if (socketstore_tail) socketstore_tail->next = next; else socketstore_head = next; socketstore_tail = next; if (! type->fork) { if ((retc = nocloseonexec(fd))) goto ret; } else if ((retc = closeonexec(fd))) goto ret; if (type->listener) { DEBUG("add: %li", fd); FD_SET(fd, &sock_fds); if (fd >= maxsocket) maxsocket = fd+1; if (recsocket < 0) recsocket = fd; if (info->listener.recsigfd != -1 || info->listener.trecsigfd != -1) { if ((retc = forksighandler(info->listener.recsigfd, info->listener.trecsigfd, info->listener.unixport, &next->redirector))) goto ret; next->info.listener.recsigfd = -1; next->info.listener.trecsigfd = -1; } } sockprintdebug("open", next); ret: if (retc) delsocket_force(fd); return retc; }
void runtime·netpollinit(void) { epfd = runtime·epollcreate1(EPOLL_CLOEXEC); if(epfd >= 0) return; epfd = runtime·epollcreate(1024); if(epfd >= 0) { runtime·closeonexec(epfd); return; } runtime·printf("netpollinit: failed to create descriptor (%d)\n", -epfd); runtime·throw("netpollinit: failed to create descriptor"); }