/* function: "write_png(bmp, path[, color_type, text_dict])\n Writes the bitmap as a png at the given path.\n Raises OSError on failure.\n \n Optional parameters:\n - color_type: A constant from the png-module (e.g. png.RGB_ALPHA)\n - text_dict: A dictionary of key-strings to value-strings\n for png tEXt meta-data." name: "write_png" */ static void write_png_py(const FilePath& p, const Bitmap& bmp, const Optional<int>& rawColorType, const Optional<png_tEXt_map>& maybeTextChunks) { const auto defaultType = static_cast<int>(PngColorType::RGB_ALPHA); const auto colorType = to_enum<PngColorType>(rawColorType.Or(defaultType)).Visit( [](const PngColorType t){ return t; }, []() -> PngColorType{ throw ValueError("color_type out of range."); }); auto r = maybeTextChunks.Visit( [&](const png_tEXt_map& textChunks){ return write_png(p, bmp, colorType, textChunks); }, [&](){ return write_png(p, bmp, colorType); }); if (!r.Successful()){ throw OSError(r.ErrorDescription()); } }
/* function: "read_png(path)->(bmp, text_dict)\n Reads the png-file at path, returning a Bitmap and a dictionary of tEXt-entries.\n Raises OSError on failure." name: "read_png" */ static png_pair read_png_py(const FilePath& path){ return read_png_meta(path).Visit( [](const Bitmap_and_tEXt& obj) -> png_pair{ return {obj.bmp, obj.text}; }, [](const utf8_string& error) -> png_pair{ throw OSError(error); }); }
void write_numpy(const string& filename,int rank,const npy_intp* dimensions,int type_num,void* data) { // Make header const auto H = fill_numpy_header(rank,dimensions,type_num); // Write npy file FILE* file = fopen(filename.c_str(),"wb"); if(!file) throw OSError("Can't open "+filename+" for writing"); fwrite(H.x.data(),1,H.x.size(),file); fwrite(data,1,H.y,file); fclose(file); }
void drop_privs(uid_t uid, gid_t gid) { if (::getuid() == (uid_t)0) { // running as root if (::setgid(gid) != 0) { throw OSError("failed to drop group privileges"); } // change the uid // use panic() rather than throw ... you can't catch+ignore panic() if (::setuid(uid) != 0) { panic("failed to drop privileges"); } } }
void wait(void) { int status; for(;;) { if (::waitpid(-1, &status, 0) == (pid_t)-1) { if (errno == ECHILD) { // no more child processes break; } throw OSError("failed waitpid() call, something wrong with child process"); } } }
void child_trampoline__(const std::function<void()>& func) { int status; switch(::fork()) { case (pid_t)-1: throw OSError("failed to fork off a child process"); break; case (pid_t)0: // child process; this is a forked copy assign_tid(); func(); std::exit(0); // terminate child process break; default: assign_tid(); if (::waitpid(-1, &status, WNOHANG) == (pid_t)-1 && errno != ECHILD) { throw OSError("failed waitpid() call, something wrong with child process"); } } }
BoundedFile::BoundedFile( const string & path, const int64_t end ) : m_sPath( path ), m_iEnd( end ), m_file( open( path.c_str(), O_RDONLY ) ), m_pBIO( BIO_new( &boundedMethods ) ) { if ( m_file < 0 ) throw OSError( "opening " + QS( path ) ); DEBUG( "bf: ctor: path=" << QS( path ) << ", end=" << end ); m_pBIO->ptr = this; m_pBIO->init = 1; }
Process::Process(const char* file, char *argv[]) { static char none[] = ""; if(argv == 0) { //For some unknow reason, 'argv = &none" is wrong: //cannot convert 'char (*)[1]' to 'char**' in an assignament char *p = none; argv = &p; } Log::debug("Spawning the process %s.", file); pid_t pid = fork(); if(pid == 0) { // children process execv(file, argv); throw OSError("The process's image '%s' cannot be loaded.", file); }else if(pid == -1) { throw OSError("The process's children cannot be created (forked)."); }else { // parent process Log::debug("Spawned the process %s (PID %i).", file, pid); this->pid = pid; } }
// launch function as thread // invoked by go(func, arg1, arg2, ...) void go_trampoline__(const std::function<void()>& func) { std::lock_guard<std::mutex> guard(thread_mgr_lock); std::atomic<int> start; start = 0; try { // keep the thread as variable in the thread_mgr array thread_mgr.push_back(std::thread(go_thread_main, func, &start)); } catch(std::system_error) { throw OSError("failed to start thread"); } // spinlock ... eek // how to improve this, without destroying locks before the thread ever // got to them, etc. ? while(start == 0); }
int BoundedFile::read( char * buf, int bufLen ) { const off64_t cur( lseek64( m_file, 0, SEEK_CUR ) ); if ( cur == static_cast< off64_t >( -1 ) ) throw OSError( "finding current offset in " + QS( m_sPath ) ); const size_t len( static_cast< size_t >( ( m_iEnd < cur + bufLen ) ? m_iEnd - cur : bufLen ) ); const int rv = static_cast< int >( ::read( m_file, buf, len ) ); DEBUG_FINE( "bf-rd: " << QS( m_sPath ) << ": " "end=" << m_iEnd << ", cur=" << cur << ", " "len=" << len << ", rv=" << rv ); return rv; }
void daemonize(bool goto_background) { if (getppid() == 1) { // if started from init, skip the most part goto_background = false; // close all file descriptors for(int fd = ::getdtablesize()-1; fd >= 0; fd--) ::close(fd); // reopen stdin, stdout, stderr as /dev/null int fd = ::open("/dev/null", O_RDWR); if (fd >= 0) { ::dup(fd); ::dup(fd); } } if (goto_background) { ::signal(SIGTTOU, SIG_IGN); ::signal(SIGTTIN, SIG_IGN); ::signal(SIGTSTP, SIG_IGN); pid_t pid = ::fork(); if (pid == (pid_t)-1) { throw OSError("failed to fork"); } if (pid > 0) { // parent exits std::exit(0); } ::setsid(); // lose controlling tty int fd = ::open("/dev/tty", O_WRONLY); if (fd >= 0) { ::ioctl(fd, TIOCNOTTY, nullptr); ::close(fd); } ::setpgrp(); // fork again pid = ::fork(); if (pid == (pid_t)-1) { throw OSError("failed to fork"); } if (pid > 0) { // parent exits std::exit(0); } // close all file descriptors for(int fd = ::getdtablesize()-1; fd >= 0; fd--) { ::close(fd); } // reopen stdin, stdout, stderr as /dev/null if ((fd = ::open("/dev/null", O_RDWR)) >= 0) { ::dup(fd); ::dup(fd); } } ::chdir("/"); ::umask(022); }
void Process::send_signal(int signal, bool expect_alive) { if(kill(pid, signal) == -1) { if(errno == ESRCH and not expect_alive) return throw OSError("Error when sending a signal -%i to the process children with PID %i", signal, pid); } }
int Process::wait() { int status = -1; if(waitpid(pid, &status, 0) == -1) throw OSError("Error when waiting for the process children with PID %i finish", pid); return status; }