Make regen work even when someone has compilation errors
[hcoop/domtool2.git] / src / main.sml
index ca57210..4e377a9 100644 (file)
@@ -1,5 +1,5 @@
 (* HCoop Domtool (http://hcoop.sourceforge.net/)
- * Copyright (c) 2006, Adam Chlipala
+ * Copyright (c) 2006-2007, Adam Chlipala
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -170,19 +170,32 @@ val dispatcher =
 val self =
     "localhost:" ^ Int.toString Config.slavePort
 
+fun context x =
+    (OpenSSL.context false x)
+    handle e as OpenSSL.OpenSSL _ =>
+          (print "Couldn't find your certificate.\nYou probably haven't been given any Domtool privileges.\n";
+           raise e)
+
 fun requestContext f =
     let
-       val uid = Posix.ProcEnv.getuid ()
-       val user = Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
+       val user =
+           case Posix.ProcEnv.getenv "DOMTOOL_USER" of
+               NONE =>
+               let
+                   val uid = Posix.ProcEnv.getuid ()
+               in
+                   Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
+               end
+             | SOME user => user
                   
        val () = Acl.read Config.aclFile
        val () = Domain.setUser user
                 
        val () = f ()
 
-       val context = OpenSSL.context (Config.certDir ^ "/" ^ user ^ ".pem",
-                                      Config.keyDir ^ "/" ^ user ^ "/key.pem",
-                                      Config.trustStore)
+       val context = context (Config.certDir ^ "/" ^ user ^ ".pem",
+                              Config.keyDir ^ "/" ^ user ^ "/key.pem",
+                              Config.trustStore)
     in
        (user, context)
     end
@@ -780,6 +793,8 @@ fun requestFirewall {node, uname} =
 
 fun regenerate context =
     let
+       val _ = ErrorMsg.reset ()
+
        val b = basis ()
        val () = Tycheck.disallowExterns ()
 
@@ -833,13 +848,16 @@ fun regenerate context =
                val (_, files) = Order.order (SOME b) files
            in
                if !ErrorMsg.anyErrors then
-                   print ("User " ^ user ^ "'s configuration has errors!\n")
+                   (ErrorMsg.reset ();
+                    print ("User " ^ user ^ "'s configuration has errors!\n"))
                else
                    app eval' files
            end
                handle IO.Io _ => ()
                     | OS.SysErr (s, _) => print ("System error processing user " ^ user ^ ": " ^ s ^ "\n")
-                    | ErrorMsg.Error => print ("User " ^ user ^ " had a compilation error.\n")
+                    | ErrorMsg.Error => (ErrorMsg.reset ();
+                                         print ("User " ^ user ^ " had a compilation error.\n"))
+                    | _ => print "Unknown exception during regeneration!\n"
     in
        app contactNode Config.nodeIps;
        Env.pre ();
@@ -883,9 +901,9 @@ fun service () =
     let
        val () = Acl.read Config.aclFile
        
-       val context = OpenSSL.context (Config.serverCert,
-                                      Config.serverKey,
-                                      Config.trustStore)
+       val context = context (Config.serverCert,
+                              Config.serverKey,
+                              Config.trustStore)
        val _ = Domain.set_context context
 
        val sock = OpenSSL.listen (context, Config.dispatcherPort)
@@ -1245,6 +1263,18 @@ fun service () =
                            OpenSSL.close bio
                            handle OpenSSL.OpenSSL _ => ();
                            loop ())
+                        | IO.Io {name, function, cause} =>
+                          (print ("IO error: " ^ function ^ " for " ^ name ^ "\n");
+                           app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory cause);
+                           OpenSSL.close bio
+                           handle OpenSSL.OpenSSL _ => ();
+                           loop ())
+                        | e =>
+                          (print "Unknown exception in main loop!\n";
+                           app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e);
+                           OpenSSL.close bio
+                           handle OpenSSL.OpenSSL _ => ();
+                           loop ())
     in
        print ("Domtool dispatcher starting up at " ^ now () ^ "\n");
        print "Listening for connections....\n";
@@ -1256,9 +1286,9 @@ fun slave () =
     let
        val host = Slave.hostname ()
 
-       val context = OpenSSL.context (Config.certDir ^ "/" ^ host ^ ".pem",
-                                      Config.keyDir ^ "/" ^ host ^ "/key.pem",
-                                      Config.trustStore)
+       val context = context (Config.certDir ^ "/" ^ host ^ ".pem",
+                              Config.keyDir ^ "/" ^ host ^ "/key.pem",
+                              Config.trustStore)
 
        val sock = OpenSSL.listen (context, Config.slavePort)
 
@@ -1311,8 +1341,9 @@ fun slave () =
                            OpenSSL.close bio
                                          handle OpenSSL.OpenSSL _ => ();
                            loop ())
-                        | OS.SysErr (s, _) =>
-                          (print ("System error: "^ s ^ "\n");
+                        | e as OS.SysErr (s, _) =>
+                          (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
+                           print ("System error: "^ s ^ "\n");
                            OpenSSL.close bio
                            handle OpenSSL.OpenSSL _ => ();
                            loop ())