-
- if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) {
- syslog(LOG_CRIT, "unable to drop privileges");
- return EXIT_FAILURE;
- }
-
- if (daemonize && daemon_detach() < 0) {
- syslog(LOG_CRIT, "unable to fork");
- return EXIT_FAILURE;
- }
-
- pidfile_refresh();
-
- if (start_listener(port) < 0)
- return EXIT_FAILURE;
-
- while (!sigint) {
- struct epoll_event evts[1024];
- int n;
-
- n = epoll_select(evts, countof(evts), -1);
- if (n < 0) {
- if (errno != EAGAIN && errno != EINTR) {
- UNIXERR("epoll_wait");
- return EXIT_FAILURE;
- }
- continue;
- }
-
- while (--n >= 0) {
- plicyd_t *d = evts[n].data.ptr;
-
- if (d->listener) {
- start_client(d);
- continue;
- }
-
- if (evts[n].events & EPOLLIN) {
- if (policy_run(d) < 0) {
- plicyd_delete(&d);
- continue;
- }
- }
-
- if ((evts[n].events & EPOLLOUT) && d->obuf.len) {
- if (buffer_write(&d->obuf, d->fd) < 0) {
- plicyd_delete(&d);
- continue;
- }
- if (!d->obuf.len) {
- epoll_modify(d->fd, EPOLLIN, d);
- }
- }
- }
- }
-
- syslog(LOG_INFO, "Stopping...");
- return EXIT_SUCCESS;