chiark / gitweb /
www-cgi/: Add some trivial tracing.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 29 Jan 2013 23:45:47 +0000 (23:45 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 2 Feb 2013 13:29:40 +0000 (13:29 +0000)
The trace goes to standard output, and only happens when debugging is
turned on, both operationally (e.g., though the `ucgi-debug' link or
USERV_U_DEBUG variable) and at compile time (with the DEBUG macro, which
you can set, e.g., with `make DEBUG="-g -DDEBUG"'.

Trace output lines start with `;;'.

I'll be grateful for this when I start shaking things up.

www-cgi/ucgi.c
www-cgi/ucgi.h
www-cgi/ucgitarget.c

index c1239d07f90b24b14f2c139d52a713e5eaa552b1..374fea5c0f1f03eef014f12f2ca649337ba23506 100644 (file)
@@ -46,18 +46,26 @@ int main(int argc, const char **argv) {
     if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout))
       syserror("write stdout");
     if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
+    D( printf(";;; UCGI\n"); )
   }
   
   if (argc > MAX_ARGS) error("too many arguments");
 
   pathi= getenv("PATH_INFO");
   if (!pathi) error("PATH_INFO not found");
+  D( if (debugmode) {
+       printf(";; find user name...\n"
+             ";;   initial PATH_INFO = `%s'\n",
+             pathi);
+  } )
   if (pathi[0] != '/' || pathi[1] != '~') error("PATH_INFO must start with /~");
   slash2= strchr(pathi+2,'/'); if (!slash2) error("PATH_INFO must have more than one /");
   usernamelen= slash2-(pathi+2);
   if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long");
   username= xmalloc(usernamelen+1);
   memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0;
+  D( if (debugmode)
+       printf(";;   user = `%s'; tail = `%s'\n", username, slash2); )
   if (!isalpha(username[0])) error("username 1st character is not alphabetic");
   xsetenv("PATH_INFO",slash2,1);
   
@@ -81,6 +89,7 @@ int main(int argc, const char **argv) {
   arguments[nargs++]= 0;
 
   if (debugmode) {
+    D( fflush(stdout); )
     child= fork(); if (child==-1) syserror("fork");
     if (child) {
       rchild= waitpid(child,&status,0);
@@ -90,6 +99,15 @@ int main(int argc, const char **argv) {
     }
   }
       
+  D( if (debugmode) {
+       int i;
+
+       printf(";; final command line...\n");
+       for (i = 0; arguments[i]; i++)
+        printf(";;   %s\n", arguments[i]);
+       fflush(stdout);
+  } )
+
   execvp("userv",(char*const*)arguments);
   syserror("exec userv");
   return -1;
index 5a51a7710de005a7aae4396241a71f0e0d9637ea..0ab5a49af428f2e9080f6eaca5e90256e0b05709 100644 (file)
 
 #include <stdlib.h>
 
+#ifdef DEBUG
+#  define D(x) x
+#else
+#  define D(x)
+#endif
+
 #define MAX_ARGS 1024
 #define MAX_USERNAME_LEN 1024
 #define MAX_SCRIPTPATH_LEN 1024
index e38ad3050aef312c72b2c6dc56cdceb95505ef25..96372353c19cbf441151a942bbe1d5be6d4e4b3b 100644 (file)
@@ -44,6 +44,7 @@ int main(int argc, const char **argv) {
   ev= getenv("USERV_U_DEBUG");
   if (ev && *ev) debugmode= 1;
   
+  D( if (debugmode) printf(";;; UCGITARGET\n"); )
   if (argc > MAX_ARGS) error("too many arguments");
 
   if (!*++argv) error("no script directory argument");
@@ -70,6 +71,11 @@ int main(int argc, const char **argv) {
   pathi= getenv("PATH_INFO");
   if (!pathi) error("PATH_INFO not found");
   lastslash= pathi;
+  D( if (debugmode) {
+       printf(";; find script name...\n"
+             ";;   PATH_INFO = `%s'\n",
+             pathi);
+  } )
   for (;;) {
     if (*lastslash != '/') error("PATH_INFO expected slash not found");
     if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
@@ -84,11 +90,13 @@ int main(int argc, const char **argv) {
     memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
     scriptpath[scriptpathlen]= 0;
     if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
+    D( if (debugmode) printf(";;   try `%s'\n", scriptpath); )
     r= stat(scriptpath,&stab); if (r) syserror("stat script");
     if (S_ISREG(stab.st_mode)) break;
     if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
     lastslash= nextslash;
   }
+  D( if (debugmode) printf(";;   found script: tail = `%s'\n", nextslash); )
   if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
   else unsetenv("PATH_INFO");
 
@@ -113,6 +121,19 @@ int main(int argc, const char **argv) {
   while ((av= (*++argv))) arguments[nargs++]= av;
   arguments[nargs++]= 0;
 
+  D( if (debugmode) {
+       int i;
+
+       printf(";; final environment...\n");
+       for (i = 0; environ[i]; i++)
+        printf(";;   %s\n", environ[i]);
+
+       printf(";; final command line...\n");
+       for (i = 0; arguments[i]; i++)
+        printf(";;   %s\n", arguments[i]);
+       fflush(stdout);
+  } )
+
   execvp(scriptpath,(char*const*)arguments);
   syserror("exec script");
   return -1;