// initialize the zmq PULL socket static void uwsgi_imperial_monitor_zeromq_init(struct uwsgi_emperor_scanner *ues) { void *context = uwsgi_zeromq_init(); ues->data = zmq_socket(context, ZMQ_PULL); if (!ues->data) { uwsgi_error("zmq_socket()"); exit(1); } if (zmq_bind(ues->data, ues->arg+6)) { uwsgi_error("zmq_socket()"); exit(1); } size_t zmq_socket_len = sizeof(int); if (zmq_getsockopt(ues->data, ZMQ_FD, &ues->fd, &zmq_socket_len) < 0) { uwsgi_error("zmq_getsockopt()"); exit(1); } ues->event_func = uwsgi_imperial_monitor_zeromq_event; event_queue_add_fd_read(uwsgi.emperor_queue, ues->fd); }
void uwsgi_zeromq_init_sockets() { uwsgi_zeromq_init(); struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; while (uwsgi_sock) { if (!uwsgi_sock->proto_name || strcmp(uwsgi_sock->proto_name, "zmq")) { goto zmq_next; } uwsgi_proto_zeromq_setup(uwsgi_sock); zmq_next: uwsgi_sock = uwsgi_sock->next; } }
// create the logpipe void create_logpipe(void) { #if defined(SOCK_SEQPACKET) && defined(__linux__) if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, uwsgi.shared->worker_log_pipe)) { #else if (socketpair(AF_UNIX, SOCK_DGRAM, 0, uwsgi.shared->worker_log_pipe)) { #endif uwsgi_error("socketpair()\n"); exit(1); } uwsgi_socket_nb(uwsgi.shared->worker_log_pipe[0]); uwsgi_socket_nb(uwsgi.shared->worker_log_pipe[1]); if (uwsgi.shared->worker_log_pipe[1] != 1) { if (dup2(uwsgi.shared->worker_log_pipe[1], 1) < 0) { uwsgi_error("dup2()"); exit(1); } } if (dup2(1, 2) < 0) { uwsgi_error("dup2()"); exit(1); } } #ifdef UWSGI_ZEROMQ // the zeromq logger ssize_t uwsgi_zeromq_logger(struct uwsgi_logger *ul, char *message, size_t len) { if (!ul->configured) { if (!ul->arg) { uwsgi_log_safe("invalid zeromq syntax\n"); exit(1); } void *ctx = uwsgi_zeromq_init(); ul->data = zmq_socket(ctx, ZMQ_PUSH); if (ul->data == NULL) { uwsgi_error_safe("zmq_socket()"); exit(1); } if (zmq_connect(ul->data, ul->arg) < 0) { uwsgi_error_safe("zmq_connect()"); exit(1); } ul->configured = 1; } zmq_msg_t msg; if (zmq_msg_init_size(&msg, len) == 0) { memcpy(zmq_msg_data(&msg), message, len); #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) zmq_sendmsg(ul->data, &msg, 0); #else zmq_send(ul->data, &msg, 0); #endif zmq_msg_close(&msg); } return 0; } #endif // log to the specified file or udp address void logto(char *logfile) { int fd; #ifdef UWSGI_UDP char *udp_port; struct sockaddr_in udp_addr; udp_port = strchr(logfile, ':'); if (udp_port) { udp_port[0] = 0; if (!udp_port[1] || !logfile[0]) { uwsgi_log("invalid udp address\n"); exit(1); } fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { uwsgi_error("socket()"); exit(1); } memset(&udp_addr, 0, sizeof(struct sockaddr_in)); udp_addr.sin_family = AF_INET; udp_addr.sin_port = htons(atoi(udp_port + 1)); char *resolved = uwsgi_resolve_ip(logfile); if (resolved) { udp_addr.sin_addr.s_addr = inet_addr(resolved); } else { udp_addr.sin_addr.s_addr = inet_addr(logfile); } if (connect(fd, (const struct sockaddr *) &udp_addr, sizeof(struct sockaddr_in)) < 0) { uwsgi_error("connect()"); exit(1); } } else { #endif if (uwsgi.log_truncate) { fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP); } else { fd = open(logfile, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP); } if (fd < 0) { uwsgi_error_open(logfile); exit(1); } uwsgi.logfile = logfile; if (uwsgi.chmod_logfile_value) { if (chmod(uwsgi.logfile, uwsgi.chmod_logfile_value)) { uwsgi_error("chmod()"); } } #ifdef UWSGI_UDP } #endif /* stdout */ if (fd != 1) { if (dup2(fd, 1) < 0) { uwsgi_error("dup2()"); exit(1); } close(fd); } /* stderr */ if (dup2(1, 2) < 0) { uwsgi_error("dup2()"); exit(1); } }