static int process_commands(struct rtpp_ctrl_sock *csock, struct cfg *cf, int controlfd, double dtime, struct rtpp_command_stats *csp, struct rtpp_stats_obj *rsc, struct rtpp_cmd_rcache_obj *rcp) { int i, rval; struct rtpp_command *cmd; int umode; umode = RTPP_CTRL_ISDG(csock); i = 0; do { cmd = get_command(cf, controlfd, &rval, dtime, csp, umode, rcp); if (cmd == NULL && rval == 0) { /* * get_command() failed with error other than I/O error * or something, there might be some good commands in * the queue. */ continue; } if (cmd != NULL) { cmd->laddr = sstosa(&csock->bindaddr); if (cmd->cca.op == GET_STATS) { flush_cstats(rsc, csp); } if (cmd->no_glock == 0) { pthread_mutex_lock(&cf->glock); } i = handle_command(cf, cmd); if (cmd->no_glock == 0) { pthread_mutex_unlock(&cf->glock); } free_command(cmd); } else { i = -1; } } while (i == 0 && umode != 0); return (i); }
static void rtpp_cmd_queue_run(void *arg) { struct rtpp_cmd_async_cf *cmd_cf; struct rtpp_cmd_pollset *psp; int i, nready, rval; double sptime; #if 0 double eptime, tused; #endif struct rtpp_command_stats *csp; struct rtpp_stats_obj *rtpp_stats_cf; cmd_cf = (struct rtpp_cmd_async_cf *)arg; rtpp_stats_cf = cmd_cf->cf_save->stable->rtpp_stats; csp = &cmd_cf->cstats; psp = &cmd_cf->pset; for (;;) { sptime = getdtime(); pthread_mutex_lock(&psp->pfds_mutex); if (psp->pfds_used == 0) { pthread_mutex_unlock(&psp->pfds_mutex); if (wait_next_clock(cmd_cf) == TSTATE_CEASE) { break; } continue; } nready = poll(psp->pfds, psp->pfds_used, 2); if (nready == 0) { pthread_mutex_unlock(&psp->pfds_mutex); if (wait_next_clock(cmd_cf) == TSTATE_CEASE) { break; } continue; } if (nready < 0 && errno == EINTR) { pthread_mutex_unlock(&psp->pfds_mutex); continue; } if (nready > 0) { for (i = 0; i < psp->pfds_used; i++) { if ((psp->pfds[i].revents & (POLLERR | POLLHUP)) != 0) { if (RTPP_CTRL_ACCEPTABLE(psp->rccs[i]->csock)) { goto closefd; } if (psp->rccs[i]->csock->type == RTPC_STDIO && (psp->pfds[i].revents & POLLIN) == 0) { goto closefd; } } if ((psp->pfds[i].revents & POLLIN) == 0) { continue; } if (RTPP_CTRL_ISSTREAM(psp->rccs[i]->csock)) { rval = process_commands_stream(cmd_cf->cf_save, psp->rccs[i], sptime, csp, rtpp_stats_cf); } else { rval = process_commands(psp->rccs[i]->csock, cmd_cf->cf_save, psp->pfds[i].fd, sptime, csp, rtpp_stats_cf, cmd_cf->rcache); } /* * Shut down non-datagram sockets that got I/O error * and also all non-continuous UNIX sockets are recycled * after each use. */ if (!RTPP_CTRL_ISDG(psp->rccs[i]->csock) && (rval == -1 || !RTPP_CTRL_ISSTREAM(psp->rccs[i]->csock))) { closefd: if (psp->rccs[i]->csock->type == RTPC_STDIO && psp->rccs[i]->csock->exit_on_close != 0) { cmd_cf->cf_save->stable->slowshutdown = 1; } rtpp_cmd_connection_dtor(psp->rccs[i]); psp->pfds_used--; if (psp->pfds_used > 0 && i < psp->pfds_used) { memcpy(&psp->pfds[i], &psp->pfds[i + 1], (psp->pfds_used - i) * sizeof(struct pollfd)); memcpy(&psp->rccs[i], &psp->rccs[i + 1], (psp->pfds_used - i) * sizeof(struct rtpp_ctrl_connection *)); } } } } pthread_mutex_unlock(&psp->pfds_mutex); if (nready > 0) { rtpp_anetio_pump(cmd_cf->cf_save->stable->rtpp_netio_cf); } #if 0 eptime = getdtime(); pthread_mutex_lock(&cmd_cf->cmd_mutex); recfilter_apply(&cmd_cf->average_load, (eptime - sptime + tused) * cmd_cf->cf_save->stable->target_pfreq); pthread_mutex_unlock(&cmd_cf->cmd_mutex); #endif flush_cstats(rtpp_stats_cf, csp); #if 0 #if RTPP_DEBUG if (last_ctick % (unsigned int)cmd_cf->cf_save->stable->target_pfreq == 0 || last_ctick < 1000) { rtpp_log_write(RTPP_LOG_DBUG, cmd_cf->cf_save->stable->glog, "rtpp_cmd_queue_run %lld sptime %f eptime %f, CSV: %f,%f,%f,%f,%f", \ last_ctick, sptime, eptime, (double)last_ctick / cmd_cf->cf_save->stable->target_pfreq, \ eptime - sptime + tused, eptime, sptime, tused); rtpp_log_write(RTPP_LOG_DBUG, cmd_cf->cf_save->stable->glog, "run %lld average load %f, CSV: %f,%f", last_ctick, \ cmd_cf->average_load.lastval * 100.0, (double)last_ctick / cmd_cf->cf_save->stable->target_pfreq, cmd_cf->average_load.lastval); } #endif #endif } }
static void init_config(struct cfg *cf, int argc, char **argv) { int ch, i, umode, stdio_mode; char *bh[2], *bh6[2], *cp, *tp[2]; const char *errmsg; struct passwd *pp; struct group *gp; double x, y; struct rtpp_ctrl_sock *ctrl_sock; bh[0] = bh[1] = bh6[0] = bh6[1] = NULL; umode = stdio_mode = 0; cf->stable->pid_file = PID_FILE; cf->stable->port_min = PORT_MIN; cf->stable->port_max = PORT_MAX; cf->stable->port_ctl = 0; cf->stable->advaddr[0] = NULL; cf->stable->advaddr[1] = NULL; cf->stable->max_ttl = SESSION_TIMEOUT; cf->stable->tos = TOS; cf->stable->rrtcp = 1; cf->stable->ttl_mode = TTL_UNIFIED; cf->stable->log_level = -1; cf->stable->log_facility = -1; cf->stable->sched_offset = 0.0; cf->stable->sched_hz = rtpp_get_sched_hz(); cf->stable->sched_policy = SCHED_OTHER; cf->stable->target_pfreq = MIN(POLL_RATE, cf->stable->sched_hz); #if RTPP_DEBUG printf("target_pfreq = %f\n", cf->stable->target_pfreq); #endif cf->stable->slowshutdown = 0; cf->timeout_handler = rtpp_th_init(); if (cf->timeout_handler == NULL) err(1, "rtpp_th_init"); pthread_mutex_init(&cf->glock, NULL); pthread_mutex_init(&cf->sessinfo.lock, NULL); pthread_mutex_init(&cf->bindaddr_lock, NULL); cf->stable->nofile_limit = malloc(sizeof(*cf->stable->nofile_limit)); if (cf->stable->nofile_limit == NULL) err(1, "malloc"); if (getrlimit(RLIMIT_NOFILE, cf->stable->nofile_limit) != 0) err(1, "getrlimit"); while ((ch = getopt(argc, argv, "vf2Rl:6:s:S:t:r:p:T:L:m:M:u:Fin:Pad:VN:c:A:")) != -1) { switch (ch) { case 'c': if (strcmp(optarg, "fifo") == 0) { cf->stable->sched_policy = SCHED_FIFO; break; } if (strcmp(optarg, "rr") == 0) { cf->stable->sched_policy = SCHED_RR; break; } errx(1, "%s: unknown scheduling policy", optarg); break; case 'N': if (strcmp(optarg, "random") == 0) { x = getdtime() * 1000000.0; srand48((long)x); cf->stable->sched_offset = drand48(); } else { tp[0] = optarg; tp[1] = strchr(tp[0], '/'); if (tp[1] == NULL) { errx(1, "%s: -N should be in the format X/Y", optarg); } *tp[1] = '\0'; tp[1]++; x = (double)strtol(tp[0], &tp[0], 10); y = (double)strtol(tp[1], &tp[1], 10); cf->stable->sched_offset = x / y; } x = (double)cf->stable->sched_hz / cf->stable->target_pfreq; cf->stable->sched_offset = trunc(x * cf->stable->sched_offset) / x; cf->stable->sched_offset /= cf->stable->target_pfreq; warnx("sched_offset = %f", cf->stable->sched_offset); break; case 'f': cf->stable->nodaemon = 1; break; case 'l': bh[0] = optarg; bh[1] = strchr(bh[0], '/'); if (bh[1] != NULL) { *bh[1] = '\0'; bh[1]++; cf->stable->bmode = 1; } break; case '6': bh6[0] = optarg; bh6[1] = strchr(bh6[0], '/'); if (bh6[1] != NULL) { *bh6[1] = '\0'; bh6[1]++; cf->stable->bmode = 1; } break; case 'A': if (*optarg == '\0') { errx(1, "first advertised address is invalid"); } cf->stable->advaddr[0] = optarg; cp = strchr(optarg, '/'); if (cp != NULL) { *cp = '\0'; cp++; if (*cp == '\0') { errx(1, "second advertised address is invalid"); } } cf->stable->advaddr[1] = cp; break; case 's': ctrl_sock = rtpp_ctrl_sock_parse(optarg); if (ctrl_sock == NULL) { errx(1, "can't parse control socket argument"); } rtpp_list_append(cf->stable->ctrl_socks, ctrl_sock); if (RTPP_CTRL_ISDG(ctrl_sock)) { umode = 1; } else if (ctrl_sock->type == RTPC_STDIO) { stdio_mode = 1; } break; case 't': cf->stable->tos = atoi(optarg); if (cf->stable->tos > 255) errx(1, "%d: TOS is too large", cf->stable->tos); break; case '2': cf->stable->dmode = 1; break; case 'v': printf("Basic version: %d\n", CPROTOVER); for (i = 1; proto_caps[i].pc_id != NULL; ++i) { printf("Extension %s: %s\n", proto_caps[i].pc_id, proto_caps[i].pc_description); } exit(0); break; case 'r': cf->stable->rdir = optarg; break; case 'S': cf->stable->sdir = optarg; break; case 'R': cf->stable->rrtcp = 0; break; case 'p': cf->stable->pid_file = optarg; break; case 'T': cf->stable->max_ttl = atoi(optarg); break; case 'L': cf->stable->nofile_limit->rlim_cur = cf->stable->nofile_limit->rlim_max = atoi(optarg); if (setrlimit(RLIMIT_NOFILE, cf->stable->nofile_limit) != 0) err(1, "setrlimit"); if (getrlimit(RLIMIT_NOFILE, cf->stable->nofile_limit) != 0) err(1, "getrlimit"); if (cf->stable->nofile_limit->rlim_max < atoi(optarg)) warnx("limit allocated by setrlimit (%d) is less than " "requested (%d)", (int) cf->stable->nofile_limit->rlim_max, atoi(optarg)); break; case 'm': cf->stable->port_min = atoi(optarg); break; case 'M': cf->stable->port_max = atoi(optarg); break; case 'u': cf->stable->run_uname = optarg; cp = strchr(optarg, ':'); if (cp != NULL) { if (cp == optarg) cf->stable->run_uname = NULL; cp[0] = '\0'; cp++; } cf->stable->run_gname = cp; cf->stable->run_uid = -1; cf->stable->run_gid = -1; if (cf->stable->run_uname != NULL) { pp = getpwnam(cf->stable->run_uname); if (pp == NULL) err(1, "can't find ID for the user: %s", cf->stable->run_uname); cf->stable->run_uid = pp->pw_uid; if (cf->stable->run_gname == NULL) cf->stable->run_gid = pp->pw_gid; } if (cf->stable->run_gname != NULL) { gp = getgrnam(cf->stable->run_gname); if (gp == NULL) err(1, "can't find ID for the group: %s", cf->stable->run_gname); cf->stable->run_gid = gp->gr_gid; } break; case 'F': cf->stable->no_check = 1; break; case 'i': cf->stable->ttl_mode = TTL_INDEPENDENT; break; case 'n': if(strncmp("unix:", optarg, 5) == 0) optarg += 5; if(strlen(optarg) == 0) errx(1, "timeout notification socket name too short"); if (rtpp_th_set_sn(cf->timeout_handler, optarg, NULL) == NULL) { err(1, "can't allocate memory"); } break; case 'P': cf->stable->record_pcap = 1; break; case 'a': cf->stable->record_all = 1; break; case 'd': cp = strchr(optarg, ':'); if (cp != NULL) { cf->stable->log_facility = rtpp_log_str2fac(cp + 1); if (cf->stable->log_facility == -1) errx(1, "%s: invalid log facility", cp + 1); *cp = '\0'; } cf->stable->log_level = rtpp_log_str2lvl(optarg); if (cf->stable->log_level == -1) errx(1, "%s: invalid log level", optarg); break; case 'V': printf("%s\n", RTPP_SW_VERSION); exit(0); break; case '?': default: usage(); } } /* No control socket has been specified, add a default one */ if (RTPP_LIST_IS_EMPTY(cf->stable->ctrl_socks)) { ctrl_sock = rtpp_ctrl_sock_parse(CMD_SOCK); if (ctrl_sock == NULL) { errx(1, "can't parse control socket: \"%s\"", CMD_SOCK); } rtpp_list_append(cf->stable->ctrl_socks, ctrl_sock); } if (cf->stable->rdir == NULL && cf->stable->sdir != NULL) errx(1, "-S switch requires -r switch"); if (cf->stable->nodaemon == 0 && stdio_mode != 0) errx(1, "stdio command mode requires -f switch"); if (cf->stable->no_check == 0 && getuid() == 0 && cf->stable->run_uname == NULL) { if (umode != 0) { errx(1, "running this program as superuser in a remote control " "mode is strongly not recommended, as it poses serious security " "threat to your system. Use -u option to run as an unprivileged " "user or -F is you want to run as a superuser anyway."); } else { warnx("WARNING!!! Running this program as superuser is strongly " "not recommended, as it may pose serious security threat to " "your system. Use -u option to run as an unprivileged user " "or -F to surpress this warning."); } } /* make sure that port_min and port_max are even */ if ((cf->stable->port_min % 2) != 0) cf->stable->port_min++; if ((cf->stable->port_max % 2) != 0) { cf->stable->port_max--; } else { /* * If port_max is already even then there is no * "room" for the RTCP port, go back by two ports. */ cf->stable->port_max -= 2; } if (!IS_VALID_PORT(cf->stable->port_min)) errx(1, "invalid value of the port_min argument, " "not in the range 1-65535"); if (!IS_VALID_PORT(cf->stable->port_max)) errx(1, "invalid value of the port_max argument, " "not in the range 1-65535"); if (cf->stable->port_min > cf->stable->port_max) errx(1, "port_min should be less than port_max"); cf->sessinfo.sessions = malloc((sizeof cf->sessinfo.sessions[0]) * (((cf->stable->port_max - cf->stable->port_min + 1)) + 1)); cf->rtp_servers = malloc((sizeof cf->rtp_servers[0]) * (((cf->stable->port_max - cf->stable->port_min + 1) * 2) + 1)); cf->sessinfo.pfds_rtp = malloc((sizeof cf->sessinfo.pfds_rtp[0]) * (((cf->stable->port_max - cf->stable->port_min + 1)) + 1)); cf->sessinfo.pfds_rtcp = malloc((sizeof cf->sessinfo.pfds_rtcp[0]) * (((cf->stable->port_max - cf->stable->port_min + 1)) + 1)); if (bh[0] == NULL && bh[1] == NULL && bh6[0] == NULL && bh6[1] == NULL) { bh[0] = "*"; } for (i = 0; i < 2; i++) { if (bh[i] != NULL && *bh[i] == '\0') bh[i] = NULL; if (bh6[i] != NULL && *bh6[i] == '\0') bh6[i] = NULL; } i = ((bh[0] == NULL) ? 0 : 1) + ((bh[1] == NULL) ? 0 : 1) + ((bh6[0] == NULL) ? 0 : 1) + ((bh6[1] == NULL) ? 0 : 1); if (cf->stable->bmode != 0) { if (bh[0] != NULL && bh6[0] != NULL) errx(1, "either IPv4 or IPv6 should be configured for external " "interface in bridging mode, not both"); if (bh[1] != NULL && bh6[1] != NULL) errx(1, "either IPv4 or IPv6 should be configured for internal " "interface in bridging mode, not both"); if (cf->stable->advaddr[0] != NULL && cf->stable->advaddr[1] == NULL) errx(1, "two advertised addresses are required for internal " "and external interfaces in bridging mode"); if (i != 2) errx(1, "incomplete configuration of the bridging mode - exactly " "2 listen addresses required, %d provided", i); } else if (i != 1) { errx(1, "exactly 1 listen addresses required, %d provided", i); } for (i = 0; i < 2; i++) { cf->stable->bindaddr[i] = NULL; if (bh[i] != NULL) { cf->stable->bindaddr[i] = host2bindaddr(cf, bh[i], AF_INET, &errmsg); if (cf->stable->bindaddr[i] == NULL) errx(1, "host2bindaddr: %s", errmsg); continue; } if (bh6[i] != NULL) { cf->stable->bindaddr[i] = host2bindaddr(cf, bh6[i], AF_INET6, &errmsg); if (cf->stable->bindaddr[i] == NULL) errx(1, "host2bindaddr: %s", errmsg); continue; } } if (cf->stable->bindaddr[0] == NULL) { cf->stable->bindaddr[0] = cf->stable->bindaddr[1]; cf->stable->bindaddr[1] = NULL; } }