From: Todd C. Miller Date: Tue, 15 Jun 2010 13:22:44 +0000 (-0400) Subject: Move setsid emulation into setsid.c X-Git-Tag: SUDO_1_7_3~62 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5ef1b181cf8a5e6079628ac75e68402abc4347c0;p=sudo Move setsid emulation into setsid.c --HG-- branch : 1.7 --- diff --git a/Makefile.in b/Makefile.in index c061b157e..f86c6508f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -108,10 +108,10 @@ SRCS = aix.c alias.c alloc.c audit.c boottime.c bsm_audit.c check.c \ getspwuid.c gettime.c glob.c goodpath.c gram.c gram.y interfaces.c \ iolog.c isblank.c lbuf.c ldap.c list.c logging.c match.c mksiglist.c \ mkstemp.c memrchr.c nanosleep.c parse.c parse_args.c pwutil.c \ - set_perms.c sigaction.c snprintf.c strcasecmp.c strerror.c strlcat.c \ - strlcpy.c strsignal.c sudo.c sudo_noexec.c sudo_edit.c sudo_nss.c \ - term.c testsudoers.c tgetpass.c toke.c toke.l tsgetgrpw.c utimes.c \ - vasgroups.c visudo.c zero_bytes.c redblack.c selinux.c sesh.c \ + set_perms.c setsid.c sigaction.c snprintf.c strcasecmp.c strerror.c \ + strlcat.c strlcpy.c strsignal.c sudo.c sudo_noexec.c sudo_edit.c \ + sudo_nss.c term.c testsudoers.c tgetpass.c toke.c toke.l tsgetgrpw.c \ + utimes.c vasgroups.c visudo.c zero_bytes.c redblack.c selinux.c sesh.c \ sudoreplay.c getdate.c getdate.y getline.c timestr.c $(AUTH_SRCS) AUTH_SRCS = auth/afs.c auth/aix_auth.c auth/bsdauth.c auth/dce.c auth/fwtk.c \ @@ -331,6 +331,8 @@ redblack.o: $(srcdir)/redblack.c $(SUDODEP) $(srcdir)/redblack.h $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/redblack.c set_perms.o: $(srcdir)/set_perms.c $(SUDODEP) $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/set_perms.c +setsid.o: $(srcdir)/setsid.c $(srcdir)/compat.h config.h + $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/setsid.c sigaction.o: $(srcdir)/sigaction.c $(srcdir)/compat.h $(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEFS) $(OPTIONS) $(srcdir)/sigaction.c siglist.o: siglist.c $(srcdir)/compat.h config.h diff --git a/config.h.in b/config.h.in index ce47eb5f8..8a86ba9a6 100644 --- a/config.h.in +++ b/config.h.in @@ -696,6 +696,9 @@ /* Define to 1 to send mail when the user is not in the sudoers file. */ #undef SEND_MAIL_WHEN_NO_USER +/* Define to 1 if the `setpgrp' function takes no argument. */ +#undef SETPGRP_VOID + /* Define to 1 if you want sudo to start a shell if given no arguments. */ #undef SHELL_IF_NO_ARGS diff --git a/configure b/configure index a2bf03478..2fdcd4cfa 100755 --- a/configure +++ b/configure @@ -14594,6 +14594,66 @@ fi done +fi +done + +for ac_func in setsid +do : + ac_fn_c_check_func "$LINENO" "setsid" "ac_cv_func_setsid" +if test "x$ac_cv_func_setsid" = x""yes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_SETSID 1 +_ACEOF + +else + + case " $LIBOBJS " in + *" setsid.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS setsid.$ac_objext" + ;; +esac + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether setpgrp takes no argument" >&5 +$as_echo_n "checking whether setpgrp takes no argument... " >&6; } +if test "${ac_cv_func_setpgrp_void+set}" = set; then : + $as_echo_n "(cached) " >&6 +else + if test "$cross_compiling" = yes; then : + as_fn_error "cannot check setpgrp when cross compiling" "$LINENO" 5 +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$ac_includes_default +int +main () +{ +/* If this system has a BSD-style setpgrp which takes arguments, + setpgrp(1, 1) will fail with ESRCH and return -1, in that case + exit successfully. */ + return setpgrp (1,1) != -1; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_run "$LINENO"; then : + ac_cv_func_setpgrp_void=no +else + ac_cv_func_setpgrp_void=yes +fi +rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \ + conftest.$ac_objext conftest.beam conftest.$ac_ext +fi + +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_func_setpgrp_void" >&5 +$as_echo "$ac_cv_func_setpgrp_void" >&6; } +if test $ac_cv_func_setpgrp_void = yes; then + +$as_echo "#define SETPGRP_VOID 1" >>confdefs.h + +fi + + fi done diff --git a/configure.in b/configure.in index 98da3fddb..005c80b8e 100644 --- a/configure.in +++ b/configure.in @@ -1889,6 +1889,10 @@ AC_CHECK_FUNCS(getline, [], [ AC_LIBOBJ(getline) AC_CHECK_FUNCS(fgetln) ]) +AC_CHECK_FUNCS(setsid, [], [ + AC_LIBOBJ(setsid) + AC_FUNC_SETPGRP +]) AC_CHECK_FUNCS(sysctl getutid getutxid, [break]) diff --git a/exec_pty.c b/exec_pty.c index 17b0fc62e..363cce074 100644 --- a/exec_pty.c +++ b/exec_pty.c @@ -721,23 +721,10 @@ exec_monitor(path, argv, envp, backchannel, rbac) * and the slave pty as the controlling terminal. * This allows us to be notified when the child has been suspended. */ -#ifdef HAVE_SETSID if (setsid() == -1) { warning("setsid"); goto bad; } -#else -# ifdef TIOCNOTTY - n = open(_PATH_TTY, O_RDWR|O_NOCTTY); - if (n >= 0) { - /* Disconnect from old controlling tty. */ - if (ioctl(n, TIOCNOTTY, NULL) == -1) - warning("cannot disconnect controlling tty"); - close(n); - } -# endif - setpgrp(0, 0); -#endif if (io_fds[SFD_SLAVE] != -1) { #ifdef TIOCSCTTY if (ioctl(io_fds[SFD_SLAVE], TIOCSCTTY, NULL) != 0) diff --git a/logging.c b/logging.c index ff225fb1e..a3f5231f3 100644 --- a/logging.c +++ b/logging.c @@ -478,18 +478,8 @@ send_mail(fmt, va_alist) } /* Daemonize - disassociate from session/tty. */ -#ifdef HAVE_SETSID if (setsid() == -1) warning("setsid"); -#else - setpgrp(0, 0); -# ifdef TIOCNOTTY - if ((fd = open(_PATH_TTY, O_RDWR, 0644)) != -1) { - ioctl(fd, TIOCNOTTY, NULL); - close(fd); - } -# endif -#endif (void) chdir("/"); if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) { (void) dup2(fd, STDIN_FILENO); diff --git a/missing.h b/missing.h index 911ad13b8..a7990c262 100644 --- a/missing.h +++ b/missing.h @@ -86,5 +86,8 @@ int unsetenv __P((const char *)); #ifndef HAVE_STRSIGNAL char *strsignal __P((int)); #endif +#ifndef HAVE_SETSID +pid_t setsid __P((void)); +#endif #endif /* _SUDO_MISSING_H */ diff --git a/setsid.c b/setsid.c new file mode 100644 index 000000000..d8bba8b30 --- /dev/null +++ b/setsid.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2010 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include + +#include +#ifdef HAVE_UNISTD_H +# include +#endif /* HAVE_UNISTD_H */ + +#include +#include + +pid_t +setsid() +{ + int fd; + +#ifdef SETPGRP_VOID + if (setpgrp() == -1) +#else + if (setpgrp(0, 0) == -1) +#endif + return -1; +# ifdef TIOCNOTTY + if ((fd = open(_PATH_TTY, O_RDWR, 0644)) != -1) { + (void) ioctl(fd, TIOCNOTTY, NULL); + close(fd); + } +# endif + return getpid(); +}