// this is the monitor for glob patterns void uwsgi_imperial_monitor_glob(struct uwsgi_emperor_scanner *ues) { glob_t g; int i; struct stat st; struct uwsgi_instance *ui_current; if (glob(ues->arg, GLOB_MARK | GLOB_NOCHECK, NULL, &g)) { uwsgi_error("glob()"); return; } for (i = 0; i < (int) g.gl_pathc; i++) { if (!uwsgi_emperor_is_valid(g.gl_pathv[i])) continue; if (stat(g.gl_pathv[i], &st)) continue; if (!S_ISREG(st.st_mode)) continue; ui_current = emperor_get(g.gl_pathv[i]); if (ui_current) { // check if uid or gid are changed, in such case, stop the instance if (uwsgi.emperor_tyrant) { if (st.st_uid != ui_current->uid || st.st_gid != ui_current->gid) { uwsgi_log("!!! permissions of file %s changed. stopping the instance... !!!\n", g.gl_pathv[i]); emperor_stop(ui_current); continue; } } // check if mtime is changed and the uWSGI instance must be reloaded if (st.st_mtime > ui_current->last_mod) { emperor_respawn(ui_current, st.st_mtime); } } else { emperor_add(ues, g.gl_pathv[i], st.st_mtime, NULL, 0, st.st_uid, st.st_gid); } } globfree(&g); // now check for removed instances struct uwsgi_instance *c_ui = ui->ui_next; while (c_ui) { if (c_ui->scanner == ues) { if (stat(c_ui->name, &st)) { emperor_stop(c_ui); } } c_ui = c_ui->ui_next; } }
void uwsgi_emperor_simple_do(struct uwsgi_emperor_scanner *ues, char *name, char *config, time_t ts, uid_t uid, gid_t gid) { if (!uwsgi_emperor_is_valid(name)) return; struct uwsgi_instance *ui_current = emperor_get(name); if (ui_current) { // check if uid or gid are changed, in such case, stop the instance if (uwsgi.emperor_tyrant) { if (uid != ui_current->uid || gid != ui_current->gid) { uwsgi_log("[emperor-tyrant] !!! permissions of vassal %s changed. stopping the instance... !!!\n", name); emperor_stop(ui_current); return; } } // check if mtime is changed and the uWSGI instance must be reloaded if (ts > ui_current->last_mod) { // make a new config (free the old one) free(ui_current->config); ui_current->config = config; ui_current->config_len = strlen(config); // always respawn (no need for amqp-style rules) emperor_respawn(ui_current, ts); } } else { // make a copy of the config as it will be freed emperor_add(ues, name, ts, uwsgi_str(config), strlen((const char *) config), uid, gid); } }
// this is the monitor for non-glob directories void uwsgi_imperial_monitor_directory(struct uwsgi_emperor_scanner *ues) { struct uwsgi_instance *ui_current; struct dirent *de; struct stat st; if (chdir(ues->arg)) { uwsgi_error("chdir()"); return; } DIR *dir = opendir("."); while ((de = readdir(dir)) != NULL) { if (!uwsgi_emperor_is_valid(de->d_name)) continue; if (stat(de->d_name, &st)) continue; if (!S_ISREG(st.st_mode)) continue; ui_current = emperor_get(de->d_name); if (ui_current) { // check if uid or gid are changed, in such case, stop the instance if (uwsgi.emperor_tyrant) { if (st.st_uid != ui_current->uid || st.st_gid != ui_current->gid) { uwsgi_log("!!! permissions of file %s changed. stopping the instance... !!!\n", de->d_name); emperor_stop(ui_current); continue; } } // check if mtime is changed and the uWSGI instance must be reloaded if (st.st_mtime > ui_current->last_mod) { emperor_respawn(ui_current, st.st_mtime); } } else { emperor_add(ues, de->d_name, st.st_mtime, NULL, 0, st.st_uid, st.st_gid); } } closedir(dir); // now check for removed instances struct uwsgi_instance *c_ui = ui->ui_next; while (c_ui) { if (c_ui->scanner == ues) { if (stat(c_ui->name, &st)) { emperor_stop(c_ui); } } c_ui = c_ui->ui_next; } }
void uwsgi_emperor_simple_do(struct uwsgi_emperor_scanner *ues, char *name, char *config, time_t ts, uid_t uid, gid_t gid, char *socket_name) { if (!uwsgi_emperor_is_valid(name)) return; struct uwsgi_instance *ui_current = emperor_get(name); if (ui_current) { // check if uid or gid are changed, in such case, stop the instance if (uwsgi.emperor_tyrant) { if (uid != ui_current->uid || gid != ui_current->gid) { uwsgi_log("[emperor-tyrant] !!! permissions of vassal %s changed. stopping the instance... !!!\n", name); emperor_stop(ui_current); return; } } // check if mtime is changed and the uWSGI instance must be reloaded if (ts > ui_current->last_mod) { // make a new config (free the old one) if needed if (config) { if (ui_current->config) free(ui_current->config); ui_current->config = uwsgi_str(config); ui_current->config_len = strlen(ui_current->config); } // reload the instance emperor_respawn(ui_current, ts); } } else { // make a copy of the config as it will be freed char *new_config = NULL; size_t new_config_len = 0; if (config) { new_config = uwsgi_str(config); new_config_len = strlen(new_config); } emperor_add(ues, name, ts, new_config, new_config_len, uid, gid, socket_name); } }
void uwsgi_imperial_monitor_amqp_event(struct uwsgi_emperor_scanner *ues) { uint64_t msgsize; char *amqp_routing_key = NULL; struct uwsgi_instance *ui_current; struct stat st; char *config = uwsgi_amqp_consume(ues->fd, &msgsize, &amqp_routing_key); if (!config) { uwsgi_log("problem with RabbitMQ server, trying reconnection...\n"); close(ues->fd); ues->fd = -1; return; } // having a routing key means the body will be mapped to a config chunk if (amqp_routing_key) { uwsgi_log("AMQP routing_key = %s\n", amqp_routing_key); ui_current = emperor_get(amqp_routing_key); if (ui_current) { // make a new config free(ui_current->config); ui_current->config = config; ui_current->config_len = msgsize; if (!msgsize) { emperor_stop(ui_current); } else { emperor_respawn(ui_current, uwsgi_now()); } goto end0; } if (msgsize > 0) { emperor_add(ues, amqp_routing_key, uwsgi_now(), config, msgsize, 0, 0); } end0: free(config); free(amqp_routing_key); return; } // no routing key means the body contains a path to a config file if (msgsize >= 0xff || !msgsize) goto end; char *config_file = uwsgi_concat2n(config, msgsize, "", 0); ui_current = emperor_get(config_file); // if non-http check for file existance if (strncmp(config_file, "http://", 7)) { if (stat(config_file, &st)) { free(config_file); if (ui_current) emperor_stop(ui_current); goto end; } if (!S_ISREG(st.st_mode)) { free(config_file); if (ui_current) emperor_stop(ui_current); goto end; } } if (ui_current) { emperor_respawn(ui_current, uwsgi_now()); } else { emperor_add(ues, config_file, uwsgi_now(), NULL, 0, 0, 0); } free(config_file); end: free(config); }
// this is the command manager static void uwsgi_imperial_monitor_zeromq_cmd(struct uwsgi_emperor_scanner *ues) { int64_t more = 0; size_t more_size = sizeof(more); int i; zmq_msg_t msg[6]; zmq_msg_init(&msg[0]); zmq_msg_init(&msg[1]); zmq_msg_init(&msg[2]); zmq_msg_init(&msg[3]); zmq_msg_init(&msg[4]); zmq_msg_init(&msg[5]); for(i=0;i<6;i++) { #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) zmq_recvmsg(ues->data, &msg[i], ZMQ_DONTWAIT); #else zmq_recv(ues->data, &msg[i], ZMQ_NOBLOCK); #endif if (zmq_getsockopt(ues->data, ZMQ_RCVMORE, &more, &more_size)) { uwsgi_error("zmq_getsockopt()"); break; } if (!more && i < 4) break; } if (i < 1) { uwsgi_log("[emperor-zeromq] bad message received (command and instance name required)\n"); return; } char *ez_cmd = zmq_msg_data(&msg[0]); size_t ez_cmd_len = zmq_msg_size(&msg[0]); char *ez_name = zmq_msg_data(&msg[1]); size_t ez_name_len = zmq_msg_size(&msg[1]); char *ez_config = NULL; size_t ez_config_len = 0; char *ez_uid = NULL; size_t ez_uid_len = 0; char *ez_gid = NULL; size_t ez_gid_len = 0; char *ez_socket_name = NULL; size_t ez_socket_name_len = 0; char *socket_name = NULL; // config if (i > 1) { ez_config = zmq_msg_data(&msg[2]); ez_config_len = zmq_msg_size(&msg[2]); } // uid if (i > 2) { ez_uid = zmq_msg_data(&msg[3]); ez_uid_len = zmq_msg_size(&msg[3]); } // gid if (i > 3) { ez_gid = zmq_msg_data(&msg[4]); ez_gid_len = zmq_msg_size(&msg[4]); } // gid if (i > 4) { ez_socket_name = zmq_msg_data(&msg[5]); ez_socket_name_len = zmq_msg_size(&msg[5]); } char *name = uwsgi_concat2n(ez_name, ez_name_len, "", 0); // ok let's start checking commands if (!uwsgi_strncmp(ez_cmd, ez_cmd_len, "touch", 5)) { char *config = NULL; if (ez_config_len > 0) { config = uwsgi_concat2n(ez_config, ez_config_len, "", 0); } uid_t vassal_uid = 0; gid_t vassal_gid = 0; if (ez_uid_len > 0) { vassal_uid = uwsgi_str_num(ez_uid, ez_uid_len); } if (ez_gid_len > 0) { vassal_gid = uwsgi_str_num(ez_gid, ez_gid_len); } if (ez_socket_name) { socket_name = uwsgi_concat2n(ez_socket_name, ez_socket_name_len, "", 0); } uwsgi_emperor_simple_do(ues, name, config, uwsgi_now(), vassal_uid, vassal_gid, socket_name); if (config) { free(config); } if (socket_name) { free(socket_name); } } // destroy an instance else if (!uwsgi_strncmp(ez_cmd, ez_cmd_len, "destroy", 7)) { struct uwsgi_instance *ui = emperor_get(name); if (!ui) { uwsgi_log("[emperor-zeromq] unknown instance \"%s\"\n", name); } else { emperor_stop(ui); } } else { uwsgi_log("[emperor-zeromq] unknown command \"%.*s\"\n", (int)ez_cmd_len, ez_cmd); } free(name); zmq_msg_close(&msg[0]); zmq_msg_close(&msg[1]); zmq_msg_close(&msg[2]); zmq_msg_close(&msg[3]); zmq_msg_close(&msg[4]); zmq_msg_close(&msg[5]); }
// this is the monitor for glob patterns void uwsgi_imperial_monitor_glob(struct uwsgi_emperor_scanner *ues) { glob_t g; int i; struct stat st; struct uwsgi_instance *ui_current; if (glob(ues->arg, GLOB_MARK | GLOB_NOCHECK, NULL, &g)) { uwsgi_error("glob()"); return; } for (i = 0; i < (int) g.gl_pathc; i++) { if (!uwsgi_emperor_is_valid(g.gl_pathv[i])) continue; if (stat(g.gl_pathv[i], &st)) continue; if (!S_ISREG(st.st_mode)) continue; ui_current = emperor_get(g.gl_pathv[i]); uid_t t_uid = st.st_uid; gid_t t_gid = st.st_gid; if (uwsgi.emperor_tyrant && uwsgi.emperor_tyrant_nofollow) { struct stat lst; if (lstat(g.gl_pathv[i], &lst)) { uwsgi_error("[emperor-tyrant]/lstat()"); if (ui_current) { uwsgi_log("!!! availability of file %s changed. stopping the instance... !!!\n", g.gl_pathv[i]); emperor_stop(ui_current); } continue; } t_uid = lst.st_uid; t_gid = lst.st_gid; } if (ui_current) { // check if uid or gid are changed, in such case, stop the instance if (uwsgi.emperor_tyrant) { if (t_uid != ui_current->uid || t_gid != ui_current->gid) { uwsgi_log("!!! permissions of file %s changed. stopping the instance... !!!\n", g.gl_pathv[i]); emperor_stop(ui_current); continue; } } // check if mtime is changed and the uWSGI instance must be reloaded if (st.st_mtime > ui_current->last_mod) { emperor_respawn(ui_current, st.st_mtime); } } else { char *socket_name = emperor_check_on_demand_socket(g.gl_pathv[i]); emperor_add(ues, g.gl_pathv[i], st.st_mtime, NULL, 0, t_uid, t_gid, socket_name); if (socket_name) free(socket_name); } } globfree(&g); // now check for removed instances struct uwsgi_instance *c_ui = ui->ui_next; while (c_ui) { if (c_ui->scanner == ues) { if (c_ui->zerg) { char *colon = strrchr(c_ui->name, ':'); if (!colon) { emperor_stop(c_ui); } else { char *filename = uwsgi_calloc(0xff); memcpy(filename, c_ui->name, colon - c_ui->name); if (stat(filename, &st)) { emperor_stop(c_ui); } free(filename); } } else { if (stat(c_ui->name, &st)) { emperor_stop(c_ui); } } } c_ui = c_ui->ui_next; } }
// this is the monitor for non-glob directories void uwsgi_imperial_monitor_directory(struct uwsgi_emperor_scanner *ues) { struct uwsgi_instance *ui_current; struct dirent *de; struct stat st; if (chdir(ues->arg)) { uwsgi_error("chdir()"); return; } DIR *dir = opendir("."); while ((de = readdir(dir)) != NULL) { if (!uwsgi_emperor_is_valid(de->d_name)) continue; if (stat(de->d_name, &st)) continue; if (!S_ISREG(st.st_mode)) continue; ui_current = emperor_get(de->d_name); uid_t t_uid = st.st_uid; gid_t t_gid = st.st_gid; if (uwsgi.emperor_tyrant && uwsgi.emperor_tyrant_nofollow) { struct stat lst; if (lstat(de->d_name, &lst)) { uwsgi_error("[emperor-tyrant]/lstat()"); if (ui_current) { uwsgi_log("!!! availability of file %s changed. stopping the instance... !!!\n", de->d_name); emperor_stop(ui_current); } continue; } t_uid = lst.st_uid; t_gid = lst.st_gid; } if (ui_current) { // check if uid or gid are changed, in such case, stop the instance if (uwsgi.emperor_tyrant) { if (t_uid != ui_current->uid || t_gid != ui_current->gid) { uwsgi_log("!!! permissions of file %s changed. stopping the instance... !!!\n", de->d_name); emperor_stop(ui_current); continue; } } // check if mtime is changed and the uWSGI instance must be reloaded if (st.st_mtime > ui_current->last_mod) { emperor_respawn(ui_current, st.st_mtime); } } else { char *socket_name = emperor_check_on_demand_socket(de->d_name); emperor_add(ues, de->d_name, st.st_mtime, NULL, 0, t_uid, t_gid, socket_name); if (socket_name) free(socket_name); } } closedir(dir); // now check for removed instances struct uwsgi_instance *c_ui = ui->ui_next; while (c_ui) { if (c_ui->scanner == ues) { if (c_ui->zerg) { char *colon = strrchr(c_ui->name, ':'); if (!colon) { emperor_stop(c_ui); } else { char *filename = uwsgi_calloc(0xff); memcpy(filename, c_ui->name, colon - c_ui->name); if (stat(filename, &st)) { emperor_stop(c_ui); } free(filename); } } else { if (stat(c_ui->name, &st)) { emperor_stop(c_ui); } } } c_ui = c_ui->ui_next; } }