Making a ts.error file for each socket ($TS_SOCKET.error)
authorviric@mandarina
Sun, 09 Nov 2008 12:53:35 +0100
changeset 251 5e0802df5788
parent 250 7798b9ac1c07
child 252 f0af96f58947
Making a ts.error file for each socket ($TS_SOCKET.error)
buglist.bug
error.c
main.h
server_start.c
--- a/buglist.bug	Sun Nov 09 12:53:01 2008 +0100
+++ b/buglist.bug	Sun Nov 09 12:53:35 2008 +0100
@@ -3,6 +3,5 @@
 18	1	open	Making a different TS_SAVELIST name on each socket/date	
 19	10	open	Killing a child ts blocked the queue	ts creates the job in a new Process Group. You can send signals\nto the whole Process Group using a negative number for kill, as\nin: "kill -- -`ts -p`"  (the first double hyphen is for sending\nthe default TERM signal, and not mixing the ID with a signal\nnumber).\n\nI finally tried this and it works. Once I accidently killed the\ncorresponding ts process instead and this hung the whole queue:\n\nts(3427)---grandpa(3428)-+-father(3429)-+-son(3430)---sleep(3431)\n                        |               `-son(3435)---sleep(3436)\n                         `-father(3432)-+-son(3433)---sleep(3434)\n                                         `-son(3437)---sleep(3438)\n\n$ kill -- -3427 #should have been 3428\n$ ts\n\nID  State   Output  E-Level  Times(r/u/s)  Command [run=1/1]\n1   queued  (file)                         grandpa\n2   queued  (file)                         grandpa\n\nKilling the correct pid 3428 didn't help ts to recover. A 'ts -C'\ndid exactly nothing. Finally 'ts -K' allowed to use ts again.\n\nJust as a note\nThomas
 20	1	open	Log the output of TS_ONFINISH, for the case it gives an error	I actually used this for debugging:\n\nexec >> $HOME/${0##*/}.out 2>&1\ndate; echo params: "$@"; echo\nset -xv\n\nand found that the script is run, but that the server worked\ndifferently from what I expected: My script checks the output\nof ts to be sure that it is run via ts and not via cmdline.\n\nAs the script gets some information as parameters, I checked\nwhether these are in the output of ts. What I expected was\nthat ts would show a line like\n\n0    finished   /tmp/ts-out.l8jhbI   0        10.05/0.00/0.00 sleep 10\n\nNow I understand that the (just finished) job is shown as\n\n0    running    /tmp/ts-out.l8jhbI                           sleep 10\n\nin this very moment. I adapted my script and now it works. :-)\n\nBTW: Why don't you just log stdout/stderr of $TS_ONFINISH?
-21	10	open	Make a ts error file for each socket	> The error file is (better: was) usless for me. I installed ts\n> via emerge and a error-file was created during install. But it\n> belonges to the user portage. Could you change this filename\n> to allow to have one error file per user?\nI could make one for each socket, yes. I'll write that.\n\n
 22	1	open	Add more information to TS_SAVELIST (from ts -i, for example)	
 23	5	open	"ts -S" could return the number of slots set.	
--- a/error.c	Sun Nov 09 12:53:01 2008 +0100
+++ b/error.c	Sun Nov 09 12:53:35 2008 +0100
@@ -60,8 +60,19 @@
 {
     int fd;
     FILE* out;
+    char *path;
+    int new_size;
+    char *new_path;
 
-    fd = open("/tmp/ts.error", O_CREAT | O_APPEND | O_WRONLY, 0600);
+    create_socket_path(&path);
+    new_size = strlen(path)+10;
+    new_path = malloc(new_size);
+
+    strncpy(new_path, path, new_size);
+    strncat(new_path, ".error", (new_size - strlen(path)) - 1);
+    free(path);
+
+    fd = open(new_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
     if (fd == -1)
         return 0;
 
@@ -72,6 +83,8 @@
         return 0;
     }
 
+    free(new_path);
+
     return out;
 }
 
--- a/main.h	Sun Nov 09 12:53:01 2008 +0100
+++ b/main.h	Sun Nov 09 12:53:35 2008 +0100
@@ -221,6 +221,7 @@
 void wait_server_up();
 int ensure_server_up();
 void notify_parent(int fd);
+void create_socket_path(char **path);
 
 /* execute.c */
 int run_job();
--- a/server_start.c	Sun Nov 09 12:53:01 2008 +0100
+++ b/server_start.c	Sun Nov 09 12:53:35 2008 +0100
@@ -19,24 +19,24 @@
 
 extern int server_socket;
 
-static char *path;
+static char *socket_path;
 
 static int fork_server();
 
-static void create_path()
+void create_socket_path(char **path)
 {
     char *tmpdir;
     char userid[20];
     int size;
 
     /* As a priority, TS_SOCKET mandates over the path creation */
-    path = getenv("TS_SOCKET");
-    if (path != 0)
+    *path = getenv("TS_SOCKET");
+    if (*path != 0)
     {
         /* We need this in our memory, for forks and future 'free'. */
-        size = strlen(path) + 1;
-        path = (char *) malloc(size);
-        strcpy(path, getenv("TS_SOCKET"));
+        size = strlen(*path) + 1;
+        *path = (char *) malloc(size);
+        strcpy(*path, getenv("TS_SOCKET"));
         return;
     }
 
@@ -53,9 +53,9 @@
     size = strlen(tmpdir) + strlen("/socket-ts.") + strlen(userid) + 1;
 
     /* Freed after preparing the socket address */
-    path = (char *) malloc(size);
+    *path = (char *) malloc(size);
 
-    sprintf(path, "%s/socket-ts.%s", tmpdir, userid);
+    sprintf(*path, "%s/socket-ts.%s", tmpdir, userid);
 }
 
 int try_connect(int s)
@@ -64,7 +64,7 @@
     int res;
 
     addr.sun_family = AF_UNIX;
-    strcpy(addr.sun_path, path);
+    strcpy(addr.sun_path, socket_path);
 
     res = connect(s, (struct sockaddr *) &addr, sizeof(addr));
     return res;
@@ -98,7 +98,7 @@
             close(1);
             close(2);
             setsid();
-            server_main(p[1], path);
+            server_main(p[1], socket_path);
             exit(0);
             break;
         case -1: /* Error */
@@ -126,7 +126,7 @@
     if (server_socket == -1)
         error("getting the server socket");
 
-    create_path();
+    create_socket_path(&socket_path);
 
     res = try_connect(server_socket);
 
@@ -139,7 +139,7 @@
         error("c: cannot connect to the server");
 
     if (errno == ECONNREFUSED)
-        unlink(path);
+        unlink(socket_path);
 
     /* Try starting the server */
     notify_fd = fork_server();
@@ -153,7 +153,7 @@
         exit(-1);
     }
 
-    free(path);
+    free(socket_path);
 
     /* Good connection on the second time */
     return 1;