Tim Waugh d66c4d
--- coreutils-6.7/src/su.c.setsid	2007-01-09 17:26:26.000000000 +0000
Tim Waugh d66c4d
+++ coreutils-6.7/src/su.c	2007-01-09 17:26:57.000000000 +0000
Tim Waugh d66c4d
@@ -176,9 +176,13 @@
Tim Waugh 9f2386
 /* If true, change some environment vars to indicate the user su'd to.  */
Tim Waugh 9f2386
 static bool change_environment;
Tim Waugh 9f2386
 
Tim Waugh 9f2386
+/* If true, then don't call setsid() with a command. */
Tim Waugh 9f2386
+int same_session = 0;
Tim Waugh 9f2386
+
Tim Waugh 9f2386
 static struct option const longopts[] =
Tim Waugh 9f2386
 {
Tim Waugh 9f2386
   {"command", required_argument, NULL, 'c'},
Tim Waugh 9f2386
+  {"session-command", required_argument, NULL, 'C'},
Tim Waugh 9f2386
   {"fast", no_argument, NULL, 'f'},
Tim Waugh 9f2386
   {"login", no_argument, NULL, 'l'},
Tim Waugh 9f2386
   {"preserve-environment", no_argument, NULL, 'p'},
Tim Waugh d66c4d
@@ -478,6 +482,8 @@
Tim Waugh eb20fc
   if (child == 0) {  /* child shell */
Tim Waugh eb20fc
   change_identity (pw);
Tim Waugh eb20fc
   pam_end(pamh, 0);
Tim Waugh 9f2386
+  if (!same_session)
Tim Waugh eb20fc
+    setsid ();
Tim Waugh eb20fc
 #endif
Tim Waugh eb20fc
 
Tim Waugh eb20fc
   if (simulate_login)
Tim Waugh d66c4d
@@ -532,13 +538,27 @@
Tim Waugh eb20fc
     sigemptyset(&action.sa_mask);
Tim Waugh eb20fc
     action.sa_flags = 0;
Tim Waugh eb20fc
     sigemptyset(&ourset);
Tim Waugh eb20fc
-    if (sigaddset(&ourset, SIGTERM)
Tim Waugh eb20fc
-        || sigaddset(&ourset, SIGALRM)
Tim Waugh eb20fc
-        || sigaction(SIGTERM, &action, NULL)
Tim Waugh eb20fc
-        || sigprocmask(SIG_UNBLOCK, &ourset, NULL)) {
Tim Waugh 9f2386
+    if (!same_session)
Tim Waugh eb20fc
+      {
Ondrej Vasik 250517
+        if (sigaddset(&ourset, SIGINT) || sigaddset(&ourset, SIGQUIT))
Ondrej Vasik 250517
+          {
Ondrej Vasik 250517
+            fprintf(stderr, "%s: signal masking malfunction\n", PROGRAM_NAME);
Ondrej Vasik 250517
+            caught = 1;
Ondrej Vasik 250517
+          }
Tim Waugh eb20fc
+      }
Tim Waugh eb20fc
+    if (!caught && (sigaddset(&ourset, SIGTERM)
Ondrej Vasik 250517
+                    || sigaddset(&ourset, SIGALRM)
Ondrej Vasik 250517
+                    || sigaction(SIGTERM, &action, NULL)
Ondrej Vasik 250517
+                    || sigprocmask(SIG_UNBLOCK, &ourset, NULL))) {
Tim Waugh eb20fc
       fprintf(stderr, "%s: signal masking malfunction\n", PROGRAM_NAME);
Tim Waugh eb20fc
       caught = 1;
Tim Waugh eb20fc
     }
Tim Waugh 9f2386
+    if (!caught && !same_session && (sigaction(SIGINT, &action, NULL)
Ondrej Vasik 250517
+                                     || sigaction(SIGQUIT, &action, NULL)))
Tim Waugh eb20fc
+      {
Ondrej Vasik 250517
+        fprintf(stderr, "%s: signal masking malfunction\n", PROGRAM_NAME);
Ondrej Vasik 250517
+        caught = 1;
Tim Waugh eb20fc
+      }
Tim Waugh eb20fc
   }
Tim Waugh eb20fc
   if (!caught) {
Tim Waugh eb20fc
     do {
Tim Waugh d66c4d
@@ -609,6 +629,8 @@
Tim Waugh 9f2386
 \n\
Tim Waugh 9f2386
   -, -l, --login               make the shell a login shell\n\
Tim Waugh d66c4d
   -c, --command=COMMAND        pass a single COMMAND to the shell with -c\n\
Tim Waugh 9f2386
+  --session-command=COMMAND    pass a single COMMAND to the shell with -c\n\
Tim Waugh 9f2386
+                               and do not create a new session\n\
Tim Waugh 9f2386
   -f, --fast                   pass -f to the shell (for csh or tcsh)\n\
Tim Waugh 9f2386
   -m, --preserve-environment   do not reset environment variables\n\
Tim Waugh 9f2386
   -p                           same as -m\n\
Tim Waugh d66c4d
@@ -631,6 +653,7 @@
Tim Waugh 9f2386
   int optc;
Tim Waugh 9f2386
   const char *new_user = DEFAULT_USER;
Tim Waugh 9f2386
   char *command = NULL;
Tim Waugh 9f2386
+  int request_same_session = 0;
Tim Waugh 9f2386
   char *shell = NULL;
Tim Waugh 9f2386
   struct passwd *pw;
Tim Waugh 9f2386
   struct passwd pw_copy;
Tim Waugh d66c4d
@@ -656,6 +679,11 @@
Ondrej Vasik 250517
           command = optarg;
Ondrej Vasik 250517
           break;
Tim Waugh 9f2386
 
Ondrej Vasik 250517
+        case 'C':
Ondrej Vasik 250517
+          command = optarg;
Ondrej Vasik 250517
+          request_same_session = 1;
Ondrej Vasik 250517
+          break;
Tim Waugh 9f2386
+
Ondrej Vasik 250517
         case 'f':
Ondrej Vasik 250517
           fast_startup = true;
Ondrej Vasik 250517
           break;
Tim Waugh d66c4d
@@ -725,6 +753,9 @@
Tim Waugh 9f2386
     }
Tim Waugh 9f2386
 #endif
Tim Waugh 9f2386
 
Tim Waugh 9f2386
+  if (request_same_session || !command || !pw->pw_uid)
Tim Waugh 9f2386
+    same_session = 1;
Tim Waugh 9f2386
+
Tim Waugh 9f2386
   if (!shell && !change_environment)
Tim Waugh 9f2386
     shell = getenv ("SHELL");
Tim Waugh 9f2386
   if (shell && getuid () != 0 && restricted_shell (pw->pw_shell))