Errors during setup post-install scripts

Igor Pechtchanski pechtcha@cs.nyu.edu
Wed Oct 16 17:07:00 GMT 2002


On Wed, 16 Oct 2002, Igor Pechtchanski wrote:

> On Wed, 16 Oct 2002, David A. Cobb wrote:
>
> > Igor Pechtchanski wrote:
> >
> > >On 16 Oct 2002, Robert Collins wrote:
> > >
> > >>On Wed, 2002-10-16 at 09:11, Max Bowsher wrote:
> > >>
> > >>>David A. Cobb wrote:
> > >>>
> > >>>>Would it be a big deal to have the various setup scripts send their
> > >>>>output to, say, /var/log/setup/SCRIPTNAME.log?
> > >>>>
> > >>>Probably not, but someone has to actually do it.
> > >>>
> > >>It's been discussed in the cygwin-apps list, at length.
> > >
> > >I'm looking into a simple-minded way to do this, and this raised at least
> > >one question: do we actually need the output of the post-install scripts
> > >flashing in front of our eyes?
> > >
> > IMO, no - the screen displays generally flash past too fast to be read
> > anyway.  Just log it!
> >
> > >If not, I can just redirect it to a file,
> > >say, /var/log/setup.log.postinstall (there's probably no need for a
> > >separate file per script).
> > >
> > Well, it needs to be fairly simple to tell which script failed - if any.
> >
> > >If we actually want the console windows, it'll take some more doing,
> > >probably a separate console tee-like application.
> > >     Igor
>
> I was planning to bracket the script output with 'Running <scriptname>'
> and 'Done <scriptname>'.  This should be enough...
>         Igor

Ok, attached is the patch to setup that redirects the output of
postinstall scripts to a file (/var/log/setup.log.postinstall), bracketing
it with timestamped "Running <scriptname>" and "Done <scriptname>".
It's currently not tied into the logging mechanism, and it only redirects
the postinstall scripts (as compared to preremove and postremove), but
both are reasonably easy to add building on this patch.
	Igor

ChangeLog:
2002-10-16  Igor Pechtchanski <pechtcha@cs.nyu.edu>

	* script.cc (run): Add lname parameter.
	Redirect output of subprocess to file.
	(run_script): Add optional lname parameter.
	* script.h (run_script): Ditto.
	* postinstall.cc (RunFindVisitor::visitFile): Pass
	filename to run_script().

-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"Water molecules expand as they grow warmer" (C) Popular Science, Oct'02, p.51

-------------- next part --------------
--- postinstall.cc-orig 2002-05-18 23:07:52.000000000 -0400
+++ postinstall.cc      2002-10-16 19:40:07.000000000 -0400
@@ -33,7 +33,7 @@ public:
   RunFindVisitor (){}
   virtual void visitFile(String const &basePath, const WIN32_FIND_DATA *theFile)
     {
-      run_script ("/etc/postinstall/", theFile->cFileName);
+      run_script ("/etc/postinstall/", theFile->cFileName, "/var/log/setup.log.postinstall");
     }
   virtual ~ RunFindVisitor () {}
 protected:
--- ./script.cc-orig	2002-02-18 08:53:08.000000000 -0500
+++ ./script.cc	2002-10-16 19:01:18.000000000 -0400
@@ -30,6 +30,8 @@ static const char *cvsid =
 #include "filemanip.h"
 #include "mount.h"
 #include "io_stream.h"
+#include <time.h>
+#include "script.h"
 
 static String sh = String();
 static const char *cmd = 0;
@@ -79,12 +81,16 @@ init_run_script ()
 }
 
 static void
-run (const char *sh, const char *args, const char *file)
+run (const char *sh, const char *args, const char *file, const char *lname)
 {
   BOOL b;
   char cmdline[_MAX_PATH];
   STARTUPINFO si;
   PROCESS_INFORMATION pi;
+  HANDLE file_out = INVALID_HANDLE_VALUE;
+  DWORD dwCPFlags = CREATE_NEW_CONSOLE;
+  BOOL bInheritHandles = FALSE;
+  DWORD nout;
 
   sprintf (cmdline, "%s %s %s", sh, args, file);
   memset (&pi, 0, sizeof (pi));
@@ -93,15 +99,59 @@ run (const char *sh, const char *args, c
   si.lpTitle = (char *) "Cygwin Setup Post-Install Script";
   si.dwFlags = STARTF_USEPOSITION;
 
-  b = CreateProcess (0, cmdline, 0, 0, 0,
-		     CREATE_NEW_CONSOLE, 0, get_root_dir ().cstr_oneuse(), &si, &pi);
+  if (lname)
+    {
+      SECURITY_ATTRIBUTES sa;
+      memset (&sa, 0, sizeof (sa));
+      sa.nLength = sizeof (sa);
+      sa.bInheritHandle = TRUE;
+      sa.lpSecurityDescriptor = NULL;
+      file_out = CreateFile (backslash (cygpath (lname)).cstr_oneuse(),
+	  FILE_APPEND_DATA, FILE_SHARE_WRITE, &sa, OPEN_ALWAYS,
+	  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
+      if (file_out != INVALID_HANDLE_VALUE)
+	{
+	  time_t now; time(&now);
+	  char buf[100];
+	  struct tm *tm = localtime (&now);
+	  strftime (buf, 1000, "%Y/%m/%d %H:%M:%S ", tm);
+	  String msg = String (buf) + "Running: " + cmdline + "\n";
+	  DWORD msgl = (DWORD) msg.size();
+	  if (WriteFile (file_out, msg.cstr_oneuse(), msgl, &nout, NULL) &&
+	      nout == msgl)
+	    {
+	      bInheritHandles = TRUE;
+	      si.dwFlags = STARTF_USESTDHANDLES;
+	      si.hStdOutput = file_out;
+	      si.hStdError = file_out;
+	      dwCPFlags = 0;
+	    }
+	}
+    }
+
+  b = CreateProcess (0, cmdline, 0, 0, bInheritHandles,
+		     dwCPFlags, 0, get_root_dir ().cstr_oneuse(), &si, &pi);
 
   if (b)
     WaitForSingleObject (pi.hProcess, INFINITE);
+
+  if (lname)
+    {
+      if (file_out != INVALID_HANDLE_VALUE)
+	{
+	  time_t now; time(&now);
+	  char buf[100];
+	  struct tm *tm = localtime (&now);
+	  strftime (buf, 1000, "%Y/%m/%d %H:%M:%S ", tm);
+	  String msg = String (buf) + "Done: " + cmdline + "\n";
+	  WriteFile (file_out, msg.cstr_oneuse(), msg.size(), &nout, NULL);
+	  CloseHandle(file_out);
+	}
+    }
 }
 
 void
-run_script (String const &dir, String const &fname)
+run_script (String const &dir, String const &fname, String const &lname)
 {
   char *ext = strrchr (fname.cstr_oneuse(), '.');
   if (!ext)
@@ -111,13 +161,13 @@ run_script (String const &dir, String co
     {
       String f2 = dir + fname;
       log (LOG_PLAIN, String ("running: ") + sh + " -c " + f2);
-      run (sh.cstr_oneuse(), "-c", f2.cstr_oneuse());
+      run (sh.cstr_oneuse(), "-c", f2.cstr_oneuse(), lname.cstr_oneuse());
     }
   else if (cmd && strcmp (ext, ".bat") == 0)
     {
       String f2 = backslash (cygpath (dir + fname));
       log (LOG_PLAIN, String ("running: ") + cmd + " /c " + f2);
-      run (cmd, "/c", f2.cstr_oneuse());
+      run (cmd, "/c", f2.cstr_oneuse(), lname.cstr_oneuse());
     }
   else
     return;
--- ./script.h-orig	2002-02-18 07:35:24.000000000 -0500
+++ ./script.h	2002-10-16 18:55:55.000000000 -0400
@@ -21,7 +21,7 @@
    we have a Bourne shell, execute it using sh.  Otherwise, if fname
    has suffix .bat, execute using cmd */
    
-void run_script (String const &dir, String const &fname);
+void run_script (String const &dir, String const &fname, String const &lname = "");
 
 /* Initialisation stuff for run_script: sh, cmd, CYGWINROOT and PATH */
 void init_run_script ();
-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list