Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tcp: bind http and htsp ports before setting uid / gid - fixes#2190
  • Loading branch information
perexg committed Aug 13, 2014
1 parent b64b629 commit 0addaf1
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/htsp_server.c
Expand Up @@ -2318,6 +2318,17 @@ htsp_init(const char *bindaddr)
htsp_server_2 = tcp_server_create(bindaddr, tvheadend_htsp_port_extra, &ops, NULL);
}

/*
*
*/
void
htsp_register(void)
{
tcp_server_register(htsp_server);
if (htsp_server_2)
tcp_server_register(htsp_server_2);
}

/**
* Fire down HTSP server
*/
Expand Down
1 change: 1 addition & 0 deletions src/htsp_server.h
Expand Up @@ -23,6 +23,7 @@
#include "dvr/dvr.h"

void htsp_init(const char *bindaddr);
void htsp_register(void);
void htsp_done(void);

void htsp_channel_update_nownext(channel_t *ch);
Expand Down
6 changes: 6 additions & 0 deletions src/http.c
Expand Up @@ -972,6 +972,12 @@ http_server_init(const char *bindaddr)
http_server = tcp_server_create(bindaddr, tvheadend_webui_port, &ops, NULL);
}

void
http_server_register(void)
{
tcp_server_register(http_server);
}

void
http_server_done(void)
{
Expand Down
1 change: 1 addition & 0 deletions src/http.h
Expand Up @@ -202,6 +202,7 @@ http_path_t *http_path_add(const char *path, void *opaque,
http_callback_t *callback, uint32_t accessmask);

void http_server_init(const char *bindaddr);
void http_server_register(void);
void http_server_done(void);

int http_access_verify(http_connection_t *hc, int mask);
Expand Down
11 changes: 8 additions & 3 deletions src/main.c
Expand Up @@ -671,9 +671,14 @@ main(int argc, char **argv)
tvhlog_init(log_level, log_options, opt_logpath);
tvhlog_set_debug(log_debug);
tvhlog_set_trace(log_trace);
tvhinfo("main", "Log started");

signal(SIGPIPE, handle_sigpipe); // will be redundant later

tcp_server_preinit(opt_ipv6);
http_server_init(opt_bindaddr); // bind to ports only
htsp_init(opt_bindaddr); // bind to ports only

/* Daemonise */
if(opt_fork) {
const char *homedir;
Expand Down Expand Up @@ -807,8 +812,8 @@ main(int argc, char **argv)
timeshift_init();
#endif

tcp_server_init(opt_ipv6);
http_server_init(opt_bindaddr);
tcp_server_init();
http_server_register();
webui_init(opt_xspf);
#if ENABLE_UPNP
upnp_server_init(opt_bindaddr);
Expand All @@ -825,7 +830,7 @@ main(int argc, char **argv)

dbus_server_start();

htsp_init(opt_bindaddr);
htsp_register();


if(opt_subscribe != NULL)
Expand Down
29 changes: 21 additions & 8 deletions src/tcp.c
Expand Up @@ -545,15 +545,12 @@ tcp_server_create
(const char *bindaddr, int port, tcp_server_ops_t *ops, void *opaque)
{
int fd, x;
tvhpoll_event_t ev;
tcp_server_t *ts;
struct addrinfo hints, *res, *ressave, *use = NULL;
char port_buf[6];
int one = 1;
int zero = 0;

memset(&ev, 0, sizeof(ev));

snprintf(port_buf, 6, "%d", port);

memset(&hints, 0, sizeof(struct addrinfo));
Expand Down Expand Up @@ -611,13 +608,25 @@ tcp_server_create
ts->serverfd = fd;
ts->ops = *ops;
ts->opaque = opaque;
return ts;
}


/**
*
*/

void tcp_server_register(void *server)
{
tcp_server_t *ts = server;
tvhpoll_event_t ev;

memset(&ev, 0, sizeof(ev));

ev.fd = fd;
ev.fd = ts->serverfd;
ev.events = TVHPOLL_IN;
ev.data.ptr = ts;
tvhpoll_add(tcp_server_poll, &ev, 1);

return ts;
}

/**
Expand Down Expand Up @@ -678,12 +687,16 @@ tcp_server_connections ( void )
pthread_t tcp_server_tid;

void
tcp_server_init(int opt_ipv6)
tcp_server_preinit(int opt_ipv6)
{
tvhpoll_event_t ev;
if(opt_ipv6)
tcp_preferred_address_family = AF_INET6;
}

void
tcp_server_init(void)
{
tvhpoll_event_t ev;
tvh_pipe(O_NONBLOCK, &tcp_server_pipe);
tcp_server_poll = tvhpoll_create(10);

Expand Down
5 changes: 4 additions & 1 deletion src/tcp.h
Expand Up @@ -49,7 +49,8 @@ typedef struct tcp_server_ops

extern int tcp_preferred_address_family;

void tcp_server_init(int opt_ipv6);
void tcp_server_preinit(int opt_ipv6);
void tcp_server_init(void);
void tcp_server_done(void);

int tcp_connect(const char *hostname, int port, const char *bindaddr,
Expand All @@ -62,6 +63,8 @@ typedef void (tcp_server_callback_t)(int fd, void *opaque,
void *tcp_server_create(const char *bindaddr, int port,
tcp_server_ops_t *ops, void *opaque);

void tcp_server_register(void *server);

void tcp_server_delete(void *server);

int tcp_read(int fd, void *buf, size_t len);
Expand Down
3 changes: 1 addition & 2 deletions src/tvhlog.c
Expand Up @@ -412,7 +412,7 @@ tvhlog_init ( int level, int options, const char *path )
tvhlog_path = path ? strdup(path) : NULL;
tvhlog_trace = NULL;
tvhlog_debug = NULL;
tvhlog_run = 0;
tvhlog_run = 1;
openlog("tvheadend", LOG_PID, LOG_DAEMON);
pthread_mutex_init(&tvhlog_mutex, NULL);
pthread_cond_init(&tvhlog_cond, NULL);
Expand All @@ -422,7 +422,6 @@ tvhlog_init ( int level, int options, const char *path )
void
tvhlog_start ( void )
{
tvhlog_run = 1;
tvhthread_create(&tvhlog_tid, NULL, tvhlog_thread, NULL);
}

Expand Down

1 comment on commit 0addaf1

@dreamcat4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll have to try this out sometime.

Please sign in to comment.