int EventNotifier::handleEvents(short int event) { int count = 0; if (event & POLLIN) { #ifdef LSEFD_AVAIL uint64_t ret; if (eventfd_read(getfd(), &ret) < 0) return LS_FAIL; if (ret > INT_MAX) count = INT_MAX; else count = (int)ret; #else char achBuf[50]; int len = 0; while ((len = read(getfd(), achBuf, sizeof(achBuf) / sizeof(char))) > 0) count += len; #endif onNotified(count); } return 0; }
int AioOutputStream::open(const char *pathname, int flags, mode_t mode) { if (m_closeRequested) m_closeRequested = 0; if (getfd() == -1) setfd(ls_fio_open(pathname, flags, mode)); return getfd(); }
EventNotifier::~EventNotifier() { #ifndef LSEFD_AVAIL if (m_fdIn != -1) close(m_fdIn); #endif if (getfd() != -1) close(getfd()); }
int HttpListener::stop() { if ( getfd() != -1 ) { LOG_INFO(( "Stop listener %s.", getAddrStr() )); HttpGlobals::getMultiplexer()->remove( this ); close( getfd() ); setfd( -1 ); return 0; } return EBADF; }
int EdStream::close() { if (getfd() != -1) { if (m_pMplex) m_pMplex->remove(this); //::shutdown( getfd(), SHUT_RDWR ); ::close(getfd()); setfd(-1); } return 0; }
int File::read ( void *buf , long numBytesToRead , long offset ) { // this return -2 if never opened, -1 on error, fd on success int fd = getfd(); if ( fd < 0 ) { g_errno = EBADENGINEER; log("disk: read: fd is negative"); return -1; } // do the read int n ; retry9: if ( offset < 0 ) n = ::read ( fd , buf , numBytesToRead ); else n = pread ( fd , buf , numBytesToRead , offset ); // valgrind if ( n < 0 && errno == EINTR ) goto retry9; // copy errno to g_errno if ( n < 0 ) g_errno = errno; // cancel blocking errors - not really errors if ( g_errno == EAGAIN ) { g_errno = 0; n = 0; } if ( n < 0 ) log("disk: read(%s) : %s" , m_filename/*s_filenames[m_vfd]*/, strerror ( g_errno ) ); return n; }
// . returns number of bytes written // . returns -1 on error // . may return < numBytesToWrite if non-blocking int File::write ( void *buf , long numBytesToWrite , long offset ) { // safety catch! if ( g_conf.m_readOnlyMode ) { logf(LOG_DEBUG,"disk: Trying to write while in " "read only mode."); return -1; } // this return -2 if never opened, -1 on error, fd on success int fd = getfd(); if ( fd < 0 ) { g_errno = EBADENGINEER; log("disk: write: fd is negative"); return -1; } // write it int n; retry21: if ( offset < 0 ) n = ::write ( fd , buf , numBytesToWrite ); else n = pwrite ( fd , buf , numBytesToWrite , offset ); // valgrind if ( n < 0 && errno == EINTR ) goto retry21; // copy errno to g_errno if ( n < 0 ) g_errno = errno; // cancel blocking errors - not really errors if ( g_errno == EAGAIN ) { g_errno = 0; n = 0; } // log an error if ( n < 0 ) log("disk: write(%s) : %s" , m_filename/*s_filenames[m_vfd]*/, strerror ( g_errno ) ); return n; }
int main(int argc, char *argv[]) { struct thread_info *th_info; pthread_t th_id = -1; int fd = -1; if (parse_args(argc, argv, &g_opts)) return -1; fd = getfd(&g_opts); if (fd < 0) { printf("Can not open file.(%d)%s\n", errno, strerror(errno)); goto out; } th_info = calloc(1, sizeof(*th_info)); th_info->fd = fd; th_info->opts = &g_opts; pthread_create(&th_id, NULL, routine, (void *)th_info); controler(fd); out: if (th_id != -1) { printf("wait thread:%lu to exit\n", th_id); pthread_join(th_id, NULL); } if (fd >= 0) { close(fd); } return 0; }
int File::read ( void *buf , int32_t numBytesToRead , int32_t offset ) { // this return -2 if never opened, -1 on error, fd on success int fd = getfd(); if ( fd < 0 ) { g_errno = EBADENGINEER; log("disk: read: fd is negative"); return -1; } // do the read int n ; retry9: if ( offset < 0 ) n = ::read ( fd , buf , numBytesToRead ); else n = pread ( fd , buf , numBytesToRead , offset ); // valgrind if ( n < 0 && errno == EINTR ) goto retry9; // update linked list //promoteInLinkedList ( this ); // copy errno to g_errno if ( n < 0 ) g_errno = errno; // cancel blocking errors - not really errors if ( g_errno == EAGAIN ) { g_errno = 0; n = 0; } if ( n < 0 ) log("disk: read(%s) : %s" , getFilename(), strerror ( g_errno ) ); return n; }
int main(int argc, char *argv[]) { int fd, num; set_progname(argv[0]); #ifndef __klibc__ setlocale(LC_ALL, ""); bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); #endif if (argc == 2 && !strcmp(argv[1], "-V")) print_version_and_exit(); if (argc != 2) { fprintf(stderr, _("usage: totextmode\n")); exit(1); } fd = getfd(NULL); num = atoi(argv[1]); if (ioctl(fd,KDSETMODE,KD_TEXT)) { perror("totextmode: KDSETMODE"); exit(1); } exit(0); }
static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd, int itab, fd_set *set) { int i = 1; if (lua_isnil(L, tab)) return max_fd; while (1) { t_socket fd; lua_pushnumber(L, i); lua_gettable(L, tab); if (lua_isnil(L, -1)) { lua_pop(L, 1); break; } fd = getfd(L); if (fd != SOCKET_INVALID) { FD_SET(fd, set); if (max_fd == SOCKET_INVALID || max_fd < fd) max_fd = fd; lua_pushnumber(L, fd); lua_pushvalue(L, -2); lua_settable(L, itab); } lua_pop(L, 1); i = i + 1; } return max_fd; }
/* * eval helper function * This function can handle the built-in commands (quit, jobs, bg, fg, kill) * If it is not a built-in command, it will return 0. Otherwise, it will * return 1. There are some smaller helper functions used to handle each * built-in command. For example, dobg(), dofg() and so on. */ int builtinCommand(struct cmdline_tokens tok) { if (!strcmp(tok.argv[0], "quit")) { exit(0); } if (!strcmp(tok.argv[0], "jobs")) { listjobs(job_list, getfd(OUT, tok.outfile)); return 1; } if (!strcmp(tok.argv[0], "bg")) { dobg(tok.argv); return 1; } if (!strcmp(tok.argv[0], "fg")) { dofg(tok.argv); return 1; } if (!strcmp(tok.argv[0], "kill")) { dokill(tok.argv); return 1; } if (!strcmp(tok.argv[0], "&")) { return 1; } return 0; }
int main(int argc, char **argv) { int fd, cons; struct { char fn, subarg; } arg; set_progname(argv[0]); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); if (argc == 2) cons = atoi(argv[1]); else cons = 0; /* current console */ if ((fd = getfd(NULL)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); arg.fn = 11; /* redirect kernel messages */ arg.subarg = cons; /* to specified console */ if (ioctl(fd, TIOCLINUX, &arg)) { kbd_error(EXIT_FAILURE, errno, "TIOCLINUX"); } return EXIT_SUCCESS; }
int main(int argc, char **argv){ int fd, cons; struct { char fn, subarg; } arg; #ifndef __klibc__ setlocale(LC_ALL, ""); bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); #endif if (argc == 2) cons = atoi(argv[1]); else cons = 0; /* current console */ fd = getfd(NULL); arg.fn = 11; /* redirect kernel messages */ arg.subarg = cons; /* to specified console */ if (ioctl(fd, TIOCLINUX, &arg)) { perror("TIOCLINUX"); exit(1); } return 0; }
static void collect_fd(lua_State *L, int tab, int itab, fd_set *set, t_socket *max_fd) { int i = 1, n = 0; /* nil is the same as an empty table */ if (lua_isnil(L, tab)) return; /* otherwise we need it to be a table */ luaL_checktype(L, tab, LUA_TTABLE); while (1) { t_socket fd; lua_pushnumber(L, i); lua_gettable(L, tab); if (lua_isnil(L, -1)) { lua_pop(L, 1); break; } /* getfd figures out if this is a socket */ fd = getfd(L); if (fd != SOCKET_INVALID) { /* make sure we don't overflow the fd_set */ if (n >= FD_SETSIZE) luaL_argerror(L, tab, "too many sockets"); FD_SET(fd, set); n++; /* keep track of the largest descriptor so far */ if (*max_fd == SOCKET_INVALID || *max_fd < fd) *max_fd = fd; /* make sure we can map back from descriptor to the object */ lua_pushnumber(L, fd); lua_pushvalue(L, -2); lua_settable(L, itab); } lua_pop(L, 1); i = i + 1; } }
int ExtConn::onInitConnected() { int error; int ret = getSockError(&error); if ((ret == -1) || (error != 0)) { if (ret != -1) errno = error; return LS_FAIL; } m_iState = PROCESSING; if (LS_LOG_ENABLED(LOG4CXX_NS::Level::DBG_LESS)) { char achSockAddr[128]; char achAddr[128] = ""; int port = 0; socklen_t len = 128; if (getsockname(getfd(), (struct sockaddr *)achSockAddr, &len) == 0) { GSockAddr::ntop((struct sockaddr *)achSockAddr, achAddr, 128); port = GSockAddr::getPort((struct sockaddr *)achSockAddr); } LS_DBG_L(this, "Connected to [%s] on local address [%s:%d]!", m_pWorker->getURL(), achAddr, port); } return 0; }
static void setoempr(uint64_t v) { int fd = getfd(O_WRONLY); if (ioctl(fd, CFIOCSOEMPR, &v) < 0) err(-1, "ioctl(CFIOCGOEMPR)"); close(fd); }
static void setplr(void) { int fd = getfd(O_WRONLY); if (ioctl(fd, CFIOCSPLR, 0) < 0) err(-1, "ioctl(CFIOCPLR)"); close(fd); }
void EventNotifier::notify() { #ifdef LSEFD_AVAIL eventfd_write(getfd(), 1); #else write(m_fdIn, "a", 1); #endif }
int AioOutputStream::close() { if (getfd() == -1) return 0; if (m_pRecv) flush(); if (m_pSend) m_closeRequested = 1; else { ls_fio_close(getfd()); setfd(-1); m_closeRequested = 0; m_flushRequested = 0; } return 0; }
void CgidConn::cleanUp() { setConnector( NULL ); setState( CLOSING ); ::shutdown( getfd(), SHUT_RDWR ); // close(); recycle(); }
int HttpListener::resume() { if ( getfd() != -1 ) { HttpGlobals::getMultiplexer()->continueRead( this ); return 0; } return EBADF; }
Channel::Channel(Poller *poller, int sockfd) : poller_(poller) , sockfd_(sockfd) , addedEvents_(false) , events_(0) , r_events_(0) { LOG_DEBUG << "Channel create,fd is " << getfd(); };
int HttpListener::suspend() { if ( getfd() != -1 ) { HttpGlobals::getMultiplexer()->suspendRead( this ); return 0; } return EBADF; }
static uint64_t getoempr(void) { uint64_t v; int fd = getfd(O_RDONLY); if (ioctl(fd, CFIOCGOEMPR, &v) < 0) err(-1, "ioctl(CFIOCGOEMPR)"); close(fd); return v; }
static uint32_t getplr(void) { uint32_t plr; int fd = getfd(O_RDONLY); if (ioctl(fd, CFIOCGPLR, &plr) < 0) err(-1, "ioctl(CFIOCGPLR)"); close(fd); return plr; }
static uint64_t getfactorypr(void) { uint64_t v; int fd = getfd(O_RDONLY); if (ioctl(fd, CFIOCGFACTORYPR, &v) < 0) err(-1, "ioctl(CFIOCGFACTORYPR)"); close(fd); return v; }
static void set_colormap(unsigned char *colormap) { int fd = getfd(NULL); /* Apply the color map to the tty via ioctl */ if (ioctl(fd, PIO_CMAP, colormap) == -1) error(EXIT_FAILURE, errno, "ioctl"); close(fd); }
int main(int argc, char *argv[]) { int fd, num, i; if (argc < 1) /* unlikely */ exit(1); set_progname(argv[0]); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE_NAME, LOCALEDIR); textdomain(PACKAGE_NAME); if (argc == 2 && !strcmp(argv[1], "-V")) print_version_and_exit(); for (i = 1; i < argc; i++) { if (!isdigit(argv[i][0])) { fprintf(stderr, _("%s: unknown option\n"), progname); exit(1); } } fd = getfd(NULL); if (argc == 1) { /* deallocate all unused consoles */ if (ioctl(fd,VT_DISALLOCATE,0)) { perror("VT_DISALLOCATE"); fprintf(stderr, _("%s: deallocating all unused consoles failed\n"), progname); exit(1); } } else for (i = 1; i < argc; i++) { num = atoi(argv[i]); if (num == 0) { fprintf(stderr, _("%s: 0: illegal VT number\n"), progname); exit(1); } else if (num == 1) { fprintf(stderr, _("%s: VT 1 is the console and cannot be deallocated\n"), progname); exit(1); } else if (ioctl(fd,VT_DISALLOCATE,num)) { perror("VT_DISALLOCATE"); fprintf(stderr, _("%s: could not deallocate console %d\n"), progname, num); exit(1); } } exit(0); }
int pagein_fadvise(const char *fname, off_t o) { int fd = getfd(fname); u64 t1; t1 = rtc(); if(posix_fadvise(fd, o * pgsz, pgsz, POSIX_FADV_WILLNEED) == -1) fprintf(stderr, "fadvise(%s, %lld, %lld, WILLNEED): %s\n", fname, (long long)o, (long long)pgsz, strerror(errno)); if(o_time) account_pagein(t1, rtc()); return 1; }