Merge pull request #375 from gfleury/master

Fix environment vars after setuid, fix daemonize option, avoid to exit process when in daemonize
This commit is contained in:
vysheng 2015-01-12 21:06:47 +03:00
commit 92f9e9f166
4 changed files with 26 additions and 7 deletions

View File

@ -110,9 +110,13 @@ extern int usfd;
extern int sfd; extern int sfd;
extern int use_ids; extern int use_ids;
extern int daemonize;
extern struct tgl_state *TLS; extern struct tgl_state *TLS;
int readline_deactivated; int readline_deactivated;
void event_incoming (struct bufferevent *bev, short what, void *_arg);
int is_same_word (const char *s, size_t l, const char *word) { int is_same_word (const char *s, size_t l, const char *word) {
return s && word && strlen (word) == l && !memcmp (s, word, l); return s && word && strlen (word) == l && !memcmp (s, word, l);
} }
@ -930,10 +934,16 @@ void do_status_offline (int arg_num, struct arg args[], struct in_ev *ev) {
} }
void do_quit (int arg_num, struct arg args[], struct in_ev *ev) { void do_quit (int arg_num, struct arg args[], struct in_ev *ev) {
if (daemonize)
event_incoming (ev->bev, BEV_EVENT_EOF, ev);
//bufferevent_free(ev->bev);
do_halt (0); do_halt (0);
} }
void do_safe_quit (int arg_num, struct arg args[], struct in_ev *ev) { void do_safe_quit (int arg_num, struct arg args[], struct in_ev *ev) {
if (daemonize)
event_incoming (ev->bev, BEV_EVENT_EOF, ev);
//bufferevent_free(ev->bev);
safe_quit = 1; safe_quit = 1;
} }

5
loop.c
View File

@ -217,6 +217,7 @@ void net_loop (void) {
if (safe_quit && !TLS->active_queries) { if (safe_quit && !TLS->active_queries) {
printf ("All done. Exit\n"); printf ("All done. Exit\n");
do_halt (0); do_halt (0);
safe_quit = 0;
} }
if (sigterm_cnt > 0) { if (sigterm_cnt > 0) {
do_halt (0); do_halt (0);
@ -601,7 +602,7 @@ static void read_incoming (struct bufferevent *bev, void *_arg) {
} }
} }
static void event_incoming (struct bufferevent *bev, short what, void *_arg) { void event_incoming (struct bufferevent *bev, short what, void *_arg) {
struct in_ev *ev = _arg; struct in_ev *ev = _arg;
if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
vlogprintf (E_WARNING, "Closing incoming connection\n"); vlogprintf (E_WARNING, "Closing incoming connection\n");
@ -620,7 +621,7 @@ static void accept_incoming (evutil_socket_t efd, short what, void *arg) {
int fd = accept (efd, (struct sockaddr *)&cli_addr, &clilen); int fd = accept (efd, (struct sockaddr *)&cli_addr, &clilen);
assert (fd >= 0); assert (fd >= 0);
struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, 0); struct bufferevent *bev = bufferevent_socket_new (TLS->ev_base, fd, BEV_OPT_CLOSE_ON_FREE);
struct in_ev *e = malloc (sizeof (*e)); struct in_ev *e = malloc (sizeof (*e));
e->bev = bev; e->bev = bev;
e->refcnt = 1; e->refcnt = 1;

10
main.c
View File

@ -493,7 +493,7 @@ int disable_auto_accept;
int wait_dialog_list; int wait_dialog_list;
char *logname; char *logname;
int daemonize; int daemonize=0;
void reopen_logs (void) { void reopen_logs (void) {
@ -568,6 +568,11 @@ int change_user_group () {
if (setuid (pw->pw_uid) < 0) { if (setuid (pw->pw_uid) < 0) {
fprintf (stderr, "change_user_group: failed to assume identity of user %s\n", username); fprintf (stderr, "change_user_group: failed to assume identity of user %s\n", username);
return -1; return -1;
} else {
pw = getpwuid(getuid());
setenv("USER", pw->pw_name, 1);
setenv("HOME", pw->pw_dir, 1);
setenv("SHELL", pw->pw_shell, 1);
} }
} }
return 0; return 0;
@ -741,6 +746,9 @@ void sig_term_handler (int signum __attribute__ ((unused))) {
} }
void do_halt (int error) { void do_halt (int error) {
if (daemonize)
return;
if (!readline_disabled) { if (!readline_disabled) {
rl_free_line_state (); rl_free_line_state ();
rl_cleanup_after_signal (); rl_cleanup_after_signal ();

View File

@ -96,7 +96,7 @@ if(open $etchandle, $etcfile)
$line =~ s/\s+$//g; $line =~ s/\s+$//g;
$line =~ s/^\s+//g; $line =~ s/^\s+//g;
next unless $line; next unless $line;
next if $line =~ /^\-[dh]/; next if $line =~ /^\-[h]/;
if($line =~ /^[^\-]/) if($line =~ /^[^\-]/)
{ {
@ -112,7 +112,7 @@ if(open $etchandle, $etcfile)
$have_l = 1 if $line =~ /^-L\s/; $have_l = 1 if $line =~ /^-L\s/;
$have_c = 1 if $line =~ /^-c\s/; $have_c = 1 if $line =~ /^-c\s/;
$have_u = 1 if $line =~ /^-u\s/; $have_u = 1 if $line =~ /^-U\s/;
push @$params, $line; push @$params, $line;
} }
@ -120,7 +120,7 @@ if(open $etchandle, $etcfile)
$params = []; $params = [];
} }
push @$params, "-u telegramd" unless($have_u); push @$params, "-U telegramd" unless($have_u);
push @$params, "-L $fd_reopened" unless($have_l); push @$params, "-L $fd_reopened" unless($have_l);
push @$params, "-c config$idl" unless($have_c); push @$params, "-c config$idl" unless($have_c);
$params = join " ", @$params; $params = join " ", @$params;