File: 80EXP_session_creation_time.patch

package info (click to toggle)
screen 4.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 5,848 kB
  • sloc: ansic: 40,015; sh: 1,791; makefile: 344; perl: 63
file content (329 lines) | stat: -rw-r--r-- 9,682 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
Author: <hesso@pool.math.tu-berlin.de>
Description: Add lookup code for the creation time of each session. Requires
digging in /proc/$pid and /proc/uptime, though, so it's definitely
no candidate for the Beautiful C contest.
Affects screen's behaviour in the following situations:
* 'screen -ls' lists available sessions sorted chronologically
* 'screen -RR' now picks the youngest session instead of an
arbitrary one
Index: screen/doc/screen.1
===================================================================
--- screen.orig/doc/screen.1	2014-04-28 03:17:56.464112915 +0200
+++ screen/doc/screen.1	2014-04-28 03:17:56.448112764 +0200
@@ -244,7 +244,7 @@
 .IR screen ,
 but prints a list of
 .I pid.tty.host
-strings identifying your
+strings and creation timestamps identifying your
 .I screen
 sessions.
 Sessions marked `detached' can be resumed with \*Qscreen \-r\*U. Those marked
@@ -342,7 +342,7 @@
 sessions in another user's directory. This requires setuid-root.
 .TP 5
 .B \-R
-attempts to resume the first detached
+attempts to resume the youngest (in terms of creation time) detached
 .I screen
 session it finds.  If successful, all other command-line options are ignored.
 If no detached session exists, starts a new session using the specified
@@ -352,6 +352,8 @@
 .I screen
 is run as a login-shell (actually screen uses \*Q\-xRR\*U in that case).
 For combinations with the \fB\-d\fP/\fB\-D\fP option see there.
+.B Note:
+Time-based session selection is a Debian addition.
 .TP 5
 .BI "\-s " program 
 sets the default shell to the program specified, instead of the value
Index: screen/doc/screen.texinfo
===================================================================
--- screen.orig/doc/screen.texinfo	2014-04-28 03:17:56.464112915 +0200
+++ screen/doc/screen.texinfo	2014-04-28 03:17:56.452112800 +0200
@@ -320,7 +320,8 @@
 @itemx -list [@var{match}]
 Do not start @code{screen}, but instead print a list of session
 identification strings (usually of the form @var{pid.tty.host};
-@pxref{Session Name}).  Sessions marked @samp{detached} can be resumed
+@pxref{Session Name}) and the corresponding creation timestamps.
+Sessions marked @samp{detached} can be resumed
 with @code{screen -r}.  Those marked @samp{attached} are running and
 have a controlling terminal.  If the session runs in multiuser mode,
 it is marked @samp{multi}.  Sessions marked as @samp{unreachable} either
@@ -406,14 +407,15 @@
 sessions in another user's directory. This requires setuid-root.
 
 @item -R
-Resume the first appropriate detached @code{screen} session.  If
-successful, all other command-line options are ignored.  If no detached
+Resume the most-recently created appropriate detached @code{screen} session.
+If successful, all other command-line options are ignored.  If no detached
 session exists, start a new session using the specified options, just as
 if @samp{-R} had not been specified.  This option is set by default if
 screen is run as a login-shell (actually screen uses @samp{-xRR} in
 that case).
 For combinations with the 
 @samp{-D}/@samp{-d} option see there.
+@samp{Note:} Time-based session selection is a Debian addition.
 
 @item -s @var{program}
 Set the default shell to be @var{program}.  By default, @code{screen}
Index: screen/extern.h
===================================================================
--- screen.orig/extern.h	2014-04-28 03:17:56.464112915 +0200
+++ screen/extern.h	2014-04-28 03:17:56.456112845 +0200
@@ -390,6 +390,8 @@
 #else
 extern int   xsnprintf __P(());
 #endif
+extern time_t SessionCreationTime __P((const char *));
+extern time_t GetUptime __P((void));
 
 
 /* acl.c */
Index: screen/misc.c
===================================================================
--- screen.orig/misc.c	2014-04-28 03:17:56.464112915 +0200
+++ screen/misc.c	2014-04-28 03:17:56.456112845 +0200
@@ -29,6 +29,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>	/* mkdir() declaration */
 #include <signal.h>
+#include <fcntl.h>
 
 #include "config.h"
 #include "screen.h"
@@ -769,3 +770,40 @@
 }
 
 #endif
+
+time_t SessionCreationTime(const char *fifo) {
+  char ppath[20];
+  int pfd;
+  char pdata[512];
+  char *jiffies;
+
+  int pid = atoi(fifo);
+  if (pid <= 0) return 0;
+  sprintf(ppath, "/proc/%u/stat", pid);
+  pfd = open(ppath, O_RDONLY);
+  if (pfd < 0) return 0;
+  while (1) {
+    int R=0, RR;
+    RR = read(pfd, pdata + R, 512-R);
+    if (RR < 0) {close(pfd); return 0;}
+    else if (RR == 0) break;
+  }
+  close(pfd);
+
+  for (pfd=21, jiffies=pdata; pfd; --pfd) {
+    jiffies = strchr(jiffies, ' ');
+    if (!jiffies) break; else ++jiffies;
+  }
+  if (!jiffies) return 0;
+
+  return atol(jiffies) / 100;
+}
+
+time_t GetUptime(void) {
+  char uptimestr[32];
+  int fd = open("/proc/uptime", O_RDONLY);
+  if (fd < 0) return 0;
+  (void)read(fd, uptimestr, 32);
+  close(fd);
+  return atol(uptimestr);
+}
Index: screen/socket.c
===================================================================
--- screen.orig/socket.c	2014-04-28 03:17:56.464112915 +0200
+++ screen/socket.c	2014-04-28 03:17:56.456112845 +0200
@@ -125,16 +125,18 @@
   int sdirlen;
   int  matchlen = 0;
   char *name, *n;
-  int firsts = -1, sockfd;
-  char *firstn = NULL;
+  int sockfd = -1;
   int nfound = 0, ngood = 0, ndead = 0, nwipe = 0, npriv = 0;
   int nperfect = 0;
+  time_t sysboot = time(NULL) - GetUptime();
   struct sent
     {
       struct sent *next;
       int mode;
       char *name;
-    } *slist, **slisttail, *sent, *nsent;
+      time_t created;
+      int good;
+    } *slist, **slisttail, *sent, *nsent, *schosen = NULL;
 
   if (match)
     {
@@ -161,7 +163,6 @@
     Panic(errno, "Cannot opendir %s", SockPath);
 
   slist = 0;
-  slisttail = &slist;
   while ((dp = readdir(dirp)))
     {
       int cmatch = 0;
@@ -173,7 +174,7 @@
 	{
 	  n = name;
 	  /* if we don't want to match digits. Skip them */
-	  if ((*match <= '0' || *match > '9') && (*n > '0' && *n <= '9'))
+	  if ((*match < '0' || *match > '9') && (*n >= '0' && *n <= '9'))
 	    {
 	      while (*n >= '0' && *n <= '9')
 		n++;
@@ -252,12 +253,24 @@
       debug("  store it.\n");
       if ((sent = (struct sent *)malloc(sizeof(struct sent))) == 0)
 	continue;
-      sent->next = 0;
       sent->name = SaveStr(name);
       sent->mode = mode;
+      sent->created = sysboot + SessionCreationTime(name);
+      sent->good = 0;
+
+      for (slisttail = &slist; *slisttail; slisttail = &((*slisttail)->next))
+	{
+	  if ((*slisttail)->created < sent->created) break;
+	}
+
+      sent->next = *slisttail;
       *slisttail = sent;
-      slisttail = &sent->next;
       nfound++;
+      if (sockfd >= 0)
+	{
+	  close(sockfd);
+	  sockfd = -1;
+	}
       sockfd = MakeClientSocket(0);
 #ifdef USE_SETEUID
       /* MakeClientSocket sets ids back to eff */
@@ -316,6 +329,7 @@
 	  (!dflag && !rflag && !xflag))
 	{
 	  close(sockfd);
+	  sockfd = -1;
 	  debug("  no!\n");
 	  npriv++;		/* a good socket that was not for us */
 	  continue;
@@ -323,19 +337,8 @@
       ngood++;
       if (cmatch)
 	nperfect++;
-      if (fdp && (firsts == -1 || (cmatch && nperfect == 1)))
-	{
-	  if (firsts != -1)
-	    close(firsts);
-	  firsts = sockfd;
-	  firstn = sent->name;
-	  debug("  taken.\n");
-	}
-      else
-        {
-	  debug("  discarded.\n");
-	  close(sockfd);
-	}
+      sent->good = 1;
+      debug("  added to list.\n");
     }
   (void)closedir(dirp);
   if (!lsflag && nperfect == 1)
@@ -356,34 +359,42 @@
 	}
       for (sent = slist; sent; sent = sent->next)
 	{
+	  char timestr[64];
+	  if (sent->created == 0)
+	    {
+	      sprintf(timestr, "???");
+	    } else {
+	      strftime(timestr, 64, "%x %X", localtime(&sent->created));
+	    }
+	  printf("\t%s\t(%s)", sent->name, timestr);
 	  switch (sent->mode)
 	    {
 	    case 0700:
-	      printf("\t%s\t(Attached)\n", sent->name);
+	      printf("\t(Attached)\n");
 	      break;
 	    case 0600:
-	      printf("\t%s\t(Detached)\n", sent->name);
+	      printf("\t(Detached)\n");
 	      break;
 #ifdef MULTIUSER
 	    case 0701:
-	      printf("\t%s\t(Multi, attached)\n", sent->name);
+	      printf("\t(Multi, attached)\n");
 	      break;
 	    case 0601:
-	      printf("\t%s\t(Multi, detached)\n", sent->name);
+	      printf("\t(Multi, detached)\n");
 	      break;
 #endif
 	    case -1:
 	      /* No trigraphs here! */
-	      printf("\t%s\t(Dead ?%c?)\n", sent->name, '?');
+	      printf("\t(Dead ?%c?)\n", '?');
 	      break;
 	    case -2:
-	      printf("\t%s\t(Removed)\n", sent->name);
+	      printf("\t(Removed)\n");
 	      break;
 	    case -3:
-	      printf("\t%s\t(Remote or dead)\n", sent->name);
+	      printf("\t(Remote or dead)\n");
 	      break;
 	    case -4:
-	      printf("\t%s\t(Private)\n", sent->name);
+	      printf("\t(Private)\n");
 	      break;
 	    }
 	}
@@ -396,13 +407,37 @@
       else
         Msg(0, m, ndead > 1 ? "s" : "", ndead > 1 ? "" : "es");	/* other args for nethack */
     }
-  if (firsts != -1)
+  for (sent = slist; sent; sent = sent->next)
     {
-      sprintf(SockPath + sdirlen, "/%s", firstn);
-      *fdp = firsts;
+      if (sent->good)
+	{
+	  schosen = sent;
+	  break;
+	}
+    }
+  if (schosen && fdp)
+    {
+      if (sockfd >= 0 && !strcmp(SockPath + sdirlen + 1, schosen->name))
+	{
+	  *fdp = sockfd;
+	  sockfd = -1;
+	}
+      else
+	{
+	  sprintf(SockPath + sdirlen, "/%s", schosen->name);
+	  *fdp = MakeClientSocket(0);
+#ifdef USE_SETEUID
+	  xseteuid(real_uid);
+	  xsetegid(real_gid);
+#endif
+	}
     }
   else
     SockPath[sdirlen] = 0;
+  if (sockfd >= 0)
+    {
+      close(sockfd);
+    }
   for (sent = slist; sent; sent = nsent)
     {
       nsent = sent->next;