// sets checkboxes by bit mask inline void set_checkbox_bits(dom::element& el, const json::value& t ) { selected_cb selected; dom::element r = el.parent(); // ATTN: I assume here that all checkboxes in the group belong to the same parent! r.find_all(&selected, "[type='checkbox'][name='%S']", el.get_attribute("name")); int m = 1, v = selected.elements.size()==1?(t.get(false)?1:0):t.get(0); for( unsigned int n = 0; n < selected.elements.size(); ++n, m <<= 1 ) { dom::element& e = selected.elements[n]; if( (v & m) != 0) e.set_state( STATE_CHECKED, 0 ) ; else e.set_state( 0, STATE_CHECKED ) ; } }
cache_pool::cache_pool(json::value const &settings) : d(new _data()) { std::string type = settings.get("cache.backend","none"); #ifndef CPPCMS_NO_CACHE if(type=="thread_shared") { if(settings.get("service.worker_processes",0)>1) throw cppcms_error( "Can't use `thread_shared' backend with more then one process ether set " "service.worker_processes to 0 or 1 or use cache.backend=\"process_shared\""); unsigned items = settings.get("cache.limit",64); d->module=impl::thread_cache_factory(items); } else if(type=="process_shared") { #if defined(CPPCMS_WIN32) throw cppcms_error("The 'process_shared' backend is not supported under MS Windows and Cygwin platforms"); #elif defined(CPPCMS_NO_PREFOK_CACHE) throw cppcms_error("The 'process_shared' backend is disabled during build procedure"); #else // has prefork cache size_t memory = settings.get("cache.memory",16384); if(memory < 64) throw cppcms_error("'process_shared' cache backend requires at least 64 KB of cache memory: cache.memory >= 64"); unsigned items = settings.get("cache.limit",memory); d->module=impl::process_cache_factory(memory*1024,items); #endif // prefork cache } else #endif // no cache if(type != "none" ) { throw cppcms_error("Unsupported cache backend `" + type + "'"); } #ifndef CPPCMS_NO_TCP_CACHE if(settings.find("cache.tcp").type()==json::is_object) { std::vector<std::string> ips=settings.get<std::vector<std::string> >("cache.tcp.ips"); std::vector<int> ports = settings.get<std::vector<int> >("cache.tcp.ports"); if(ips.empty() || ports.empty() || ips.size()!=ports.size()) { throw cppcms_error("Invalid settings in cache.tcp.ports or cache.tcp.ips"); } booster::intrusive_ptr<impl::base_cache> l1=d->module; d->module=impl::tcp_cache_factory(ips,ports,l1); } #endif // no tcp cace }
inline void set_radio_index( dom::element& el, const json::value& t ) { selected_cb selected; dom::element r = el.parent(); // ATTN: I assume here that all radios in the group belong to the same parent! r.find_all(&selected, "[type='radio'][name='%S']", el.get_attribute("name")); unsigned int idx = (unsigned int)t.get(0); for( unsigned int n = 0; n < selected.elements.size(); ++n ) { dom::element& e = selected.elements[n]; if ( n == idx) { e.set_value(json::value(true)); break; } } }
/** Set value of the DOM element. Sets value for elements recognized by get_ctl_type() function. * \param[in] el \b const dom::element&, The element. * \param[in] v \b const json::value&, The value. **/ inline void set_value(dom::element& el, const json::value& v ) { switch(get_ctl_type(el)) { case CTL_UNKNOWN: break; case CTL_EDIT: case CTL_DECIMAL: case CTL_CURRENCY: case CTL_PASSWORD: case CTL_NUMERIC: case CTL_PROGRESS: case CTL_SLIDER: case CTL_SELECT_SINGLE: case CTL_SELECT_MULTIPLE: case CTL_DD_SELECT: case CTL_TEXTAREA: case CTL_DATE: case CTL_CALENDAR: case CTL_HIDDEN: default: el.set_value(v); break; // special cases: case CTL_BUTTON: break; case CTL_CHECKBOX: set_checkbox_bits(el,v); break; case CTL_RADIO: set_radio_index(el,v); break; case CTL_HTMLAREA: { aux::w2utf utf8(static_cast<const wchar_t*>(v.get( L"" ))); el.set_html( utf8, utf8.length() ); //el.update(); } break; case CTL_NO: assert(false); break; } }
json::value main_frame::set_glass(json::value on_off) { json::value pv = get_glass(); switch_glass(on_off.get(false)); return pv; }
void daemonizer::daemonize(json::value const &conf) { if(getppid() == 1) return; if(!conf.get("daemon.enable",false)) return; int gid = -1; int uid = -1; int devnull_fd = -1; int lock_fd = -1; try { devnull_fd = open("/dev/null",O_RDWR); if(devnull_fd < 0) { int err = errno; throw cppcms_error(err,"Failed to open /dev/null"); } std::string group = conf.get("daemon.group",""); if(!group.empty()) { struct group *gr = getgrnam(group.c_str()); if(!gr) throw cppcms_error("Invalid group " + group); gid = gr->gr_gid; } std::string user = conf.get("daemon.user",""); if(!user.empty()) { struct passwd *pw = getpwnam(user.c_str()); if(!pw) throw cppcms_error("Invalid user " + user); uid = pw->pw_uid; if(gid==-1) gid = pw->pw_gid; } std::string chroot_dir = conf.get("daemon.chroot",""); if(!chroot_dir.empty()) { global_urandom_fd = open("/dev/urandom",O_RDONLY); if(global_urandom_fd < 0) { int err = errno; throw cppcms_error(err,"Failed to open /dev/urandom"); } if(chdir(chroot_dir.c_str()) < 0) { int err = errno; throw cppcms_error(err,"Failed to chdir to " + chroot_dir); } if(chroot(chroot_dir.c_str()) < 0) { int err = errno; throw cppcms_error(err,"Failed to chroot to " + chroot_dir); } } int fd_limit = conf.get("daemon.fdlimit",-1); if(fd_limit != -1) { struct rlimit lm; lm.rlim_cur = fd_limit; lm.rlim_max = fd_limit; if(setrlimit(RLIMIT_NOFILE,&lm) < 0) { int err = errno; throw cppcms_error(err,"Failed to change daemon.fdlimit"); } } if(gid!=-1) { if(setgid(gid) < 0) { int err = errno; throw cppcms_error(err,"Failed setgid to group " + group); } } if(uid!=-1) { if(setuid(uid) < 0) { int err = errno; throw cppcms_error(err,"Failed setuid to user " + user); } } std::string lock_file = conf.get("daemon.lock",""); if(!lock_file.empty()) { if(lock_file[0]!='/') { char *wd = 0; std::vector<char> buf(256); while((wd=getcwd(&buf.front(),buf.size()))==0 && errno==ERANGE) buf.resize(buf.size()*2); if(!wd) { int err=errno; throw cppcms_error(err,"getcwd failed"); } lock_file=wd + ("/" + lock_file); } lock_fd = ::open(lock_file.c_str(),O_RDWR | O_CREAT | O_EXCL,0666); if(lock_fd < 0) { int err = errno; throw cppcms_error(err,"Failed to create a lock file"); } unlink_file = lock_file; } // forking twice int chid = fork(); if(chid < 0) { int err = errno; throw cppcms_error(err,"Fork failed"); } if(chid != 0) exit(0); real_pid = getpid(); if(setsid() < 0) { int err = errno; throw cppcms_error(err,"setsid failed"); } chid = fork(); if(chid < 0) { int err = errno; throw cppcms_error(err,"2nd fork failed"); } if(chid != 0) exit(0); real_pid = getpid(); dup2(devnull_fd,0); dup2(devnull_fd,1); dup2(devnull_fd,2); close(devnull_fd); devnull_fd = -1; int chdir_r = chdir("/"); (void)(chdir_r); // shut up GCC if(lock_fd!=-1) { std::ostringstream ss; ss.imbue(std::locale::classic()); ss << real_pid; std::string pid = ss.str(); int v = write(lock_fd,pid.c_str(),pid.size()); if(v!=int(pid.size())) { throw cppcms_error("Failed to write to lock file"); } close(lock_fd); lock_fd = -1; } } catch(...) { if(lock_fd >= 0) close(lock_fd); if(devnull_fd >=0) close(devnull_fd); if(global_urandom_fd >= 0) close(global_urandom_fd); throw; } }