[patch] sysutils/screen print the PID of a session created in "detached" mode

"Pietro Cerutti" <[email protected]> Sat, 22 Dec 2007 00:12:39 +0100
Newsgroups gmane.comp.gnu.screen
Message-ID <1198278759.35483__39333.9436887347$1238088981$gmane$org@gahrtop.localhost>
>Submitter-Id:	current-users 
>Originator:	Pietro Cerutti 
>Organization:	 
>Confidential:	no 
>Synopsis:	[patch] sysutils/screen print the PID of a session created in "detached" mode 
>Severity:	non-critical 
>Priority:	low 
>Category:	ports 
>Class:		change-request 
>Release:	FreeBSD 8.0-CURRENT i386 
>Environment:


System: FreeBSD 8.0-CURRENT #18: Tue Dec 18 12:48:22 CET 2007
    [email protected]:/usr/obj/usr/src/sys/MSI1034



>Description:


The patch below adds the -P option to screen.
This option is only used in conjunction with '-d -m' to print the PID of the screen session which has been created in "detached" mode.

Example:

> screen -dmP
35532
> screen -list
There is a screen on:
        35532..hostname  (Detached)
1 Socket in /tmp/screens/S-username.


>How-To-Repeat:





>Fix:


--- _screen.diff begins here ---
--- Makefile.orig	2007-12-22 00:04:19.000000000 +0100
+++ Makefile	2007-12-22 00:04:26.000000000 +0100
@@ -7,7 +7,7 @@
 
 PORTNAME=	screen
 PORTVERSION=	4.0.3
-PORTREVISION=	1
+PORTREVISION=	2
 CATEGORIES=	sysutils
 MASTER_SITES=	ftp://ftp.uni-erlangen.de/pub/utilities/screen/ \
 		http://komquats.com/distfiles/ \
--- /dev/null	2007-12-22 00:04:29.000000000 +0100
+++ files/patch-screen.c	2007-12-21 23:51:03.000000000 +0100
@@ -0,0 +1,33 @@
+--- screen.c.orig	2007-12-21 23:41:32.000000000 +0100
++++ screen.c	2007-12-21 23:49:26.000000000 +0100
+@@ -340,6 +340,7 @@
+   char *av0;
+   char socknamebuf[2 * MAXSTR];
+   int mflag = 0;
++  int printpid = 0;
+   char *myname = (ac == 0) ? "screen" : av[0];
+   char *SockDir;
+   struct stat st;
+@@ -522,6 +523,9 @@
+ 		    }
+ 		  ap = NULL;
+ 		  break;
++      case 'P': /* print PID */
++         printpid = 1;
++         break;
+ #ifdef HAVE_BRAILLE
+ 		case 'B':
+ 		  bd.bd_start_braille = 1;
+@@ -1141,8 +1145,11 @@
+     case 0:
+       break;
+     default:
+-      if (detached)
++      if (detached) {
++        if (printpid)
++          printf("%d\n", MasterPid);
+         exit(0);
++      }
+       if (SockMatch)
+ 	sprintf(socknamebuf, "%d.%s", MasterPid, SockMatch);
+       else
--- /dev/null	2007-12-22 00:04:29.000000000 +0100
+++ files/patch-help.c	2007-12-21 23:56:03.000000000 +0100
@@ -0,0 +1,10 @@
+--- help.c.orig	2007-12-21 23:54:57.000000000 +0100
++++ help.c	2007-12-21 23:55:53.000000000 +0100
+@@ -70,6 +70,7 @@
+   printf("-m            ignore $STY variable, do create a new screen session.\n");
+   printf("-O            Choose optimal output rather than exact vt100 emulation.\n");
+   printf("-p window     Preselect the named window if it exists.\n");
++  printf("-P            Print PID of detached screen (when used with -d and -m).\n");
+   printf("-q            Quiet startup. Exits with non-zero return code if unsuccessful.\n");
+   printf("-r            Reattach to a detached screen process.\n");
+   printf("-R            Reattach if possible, otherwise start a new session.\n");
--- /dev/null	2007-12-22 00:04:29.000000000 +0100
+++ files/patch-doc_screen.1	2007-12-21 23:58:30.000000000 +0100
@@ -0,0 +1,15 @@
+--- doc/screen.1.orig	2007-12-21 23:57:50.000000000 +0100
++++ doc/screen.1	2007-12-21 23:57:59.000000000 +0100
+@@ -266,10 +266,10 @@
+ session or not. This flag has a special meaning in connection
+ with the `-d' option:
+ .TP 8
+-.B \-d \-m
++.B \-d \-m (\-P)
+ Start
+ .I screen
+-in \*Qdetached\*U mode. This creates a new session but doesn't
++in \*Qdetached\*U mode (and print the PID of the created session). This creates a new session but doesn't
+ attach to it. This is useful for system startup scripts.
+ .TP 8
+ .B \-D \-m
--- /dev/null	2007-12-22 00:04:29.000000000 +0100
+++ files/patch-doc_screen.texinfo	2007-12-22 00:00:56.000000000 +0100
@@ -0,0 +1,13 @@
+--- doc/screen.texinfo.orig	2007-12-21 23:58:43.000000000 +0100
++++ doc/screen.texinfo	2007-12-22 00:00:31.000000000 +0100
+@@ -351,6 +351,10 @@
+ selects the blank window. As a special case for reattach, @samp{=}
+ brings up the windowlist on the blank window.
+ 
++@item -P
++Print the PID of detached screen. This is meant to be used in
++conjunction with @samp{-d -m}.
++
+ @item -q
+ Suppress printing of error messages. In combination with @samp{-ls} the exit 
+ value is set as follows: 9 indicates a directory without sessions. 10 
--- _screen.diff ends here ---