Branch: Tag:

1999-12-20

1999-12-20 11:44:43 by Martin Nilsson <mani@lysator.liu.se>

Some debug code and type fixes

Rev: server/base_server/roxen.pike:1.372

1:   /* -  * $Id: roxen.pike,v 1.371 1999/12/19 16:06:11 nilsson Exp $ -  * +     * The Roxen Challenger main program.    *    * Per Hedbor, Henrik Grubbström, Pontus Hagland, David Hedbor and others.    */      // ABS and suicide systems contributed freely by Francesco Chemolli - constant cvs_version="$Id: roxen.pike,v 1.371 1999/12/19 16:06:11 nilsson Exp $"; + constant cvs_version="$Id: roxen.pike,v 1.372 1999/12/20 11:44:43 nilsson Exp $";      object backend_thread;   ArgCache argcache;
27:   inherit "language";   inherit "supports";    + // --- Debug defins --- +    // #define SSL3_DEBUG -  + // #define PRIVS_DEBUG + // #define THREAD_DEBUG + // #define FD_DEBUG + // #define DUMP_DEBUG    -  + #ifdef SSL3_DEBUG + # define SSL3WERROR(X) werror("SSL3: "+X+"\n") + #else + # define SSL3WERROR(X) + #endif +  + #ifdef THREAD_DEBUG + # define THREAD_WERROR(X) werror("Thread: "+X+"\n") + #else + # define THREAD_WERROR(X) + #endif +    /*    * Version information    */
300: Inside #if defined(HAVE_EFFECTIVE_USER) and #if defined(HPUX_KLUDGE)
   * HPUX's libc also insists on filling numeric fields it doesn't like    * with the value 60001!    */ -  perror("Privs: WARNING: Assuming nobody-group.\n" +  report_debug("Privs: WARNING: Assuming nobody-group.\n"    "Trying some alternatives...\n");    // Assume we want the nobody group, and try a couple of alternatives    foreach(({ 60001, 65534, -2 }), gid2) { -  perror("%d... ", gid2); +  report_debug("%d... ", gid2);    if (initgroups(u[0], gid2) >= 0) {    if ((err = setegid(new_gid = gid2)) >= 0) { -  perror("Success!\n"); +  report_debug("Success!\n");    break;    }    }
315: Inside #if defined(HAVE_EFFECTIVE_USER)
   }   #endif /* HPUX_KLUDGE */    if (err < 0) { -  perror("Privs: Failed\n"); +  report_debug("Privs: Failed\n");    throw(({ sprintf("Failed to set EGID to %d\n", gid), backtrace() }));    } -  perror("Privs: WARNING: Set egid to %d instead of %d.\n", +  report_debug("Privs: WARNING: Set egid to %d instead of %d.\n",    gid2, gid);    gid = gid2;    }
371: Inside #if defined(HAVE_EFFECTIVE_USER)
      if(getuid()) return;    - #ifdef DEBUG + #ifdef PRIVS_DEBUG    int uid = geteuid();    if (uid != new_uid) { -  report_warning(sprintf("Privs: UID #%d differs from expected #%d\n" +  werror("Privs: UID #%d differs from expected #%d\n"    "%s\n", -  uid, new_uid, describe_backtrace(backtrace()))); +  uid, new_uid, describe_backtrace(backtrace()));    }    int gid = getegid();    if (gid != new_gid) { -  report_warning(sprintf("Privs: GID #%d differs from expected #%d\n" +  werror("Privs: GID #%d differs from expected #%d\n"    "%s\n", -  gid, new_gid, describe_backtrace(backtrace()))); +  gid, new_gid, describe_backtrace(backtrace()));    } - #endif /* DEBUG */ + #endif /* PRIVS_DEBUG */       seteuid(0);    array u = getpwuid(saved_uid);
409:    return Privs(r, u, g);   }    - #ifdef MODULE_DEBUG - #define MD_PERROR(X) roxen_perror X; - #else - #define MD_PERROR(X) - #endif /* MODULE_DEBUG */ -  +    #ifndef THREADS   class container   {
467:    configurations->stop();    int pid;    if (exit_code) { -  roxen_perror("Restarting Roxen.\n"); +  report_debug("Restarting Roxen.\n");    } else { -  roxen_perror("Shutting down Roxen.\n"); +  report_debug("Shutting down Roxen.\n");    // exit(0);    }    };
508:    * THREADS code starts here    */   #ifdef THREADS - // #define THREAD_DEBUG +       object do_thread_create(string id, function f, mixed ... args)   {    object t = thread_create(f, @args);    catch(t->set_name( id )); - #ifdef THREAD_DEBUG -  roxen_perror(id+" started\n"); - #endif +  THREAD_WERROR(id+" started");    return t;   }   
534: Inside #if defined(THREADS)
   {    if(q=catch {    do { - #ifdef THREAD_DEBUG -  werror("Handle thread ["+id+"] waiting for next event\n"); - #endif /* THREAD_DEBUG */ +  THREAD_WERROR("Handle thread ["+id+"] waiting for next event");    if((h=handle_queue->read()) && h[0]) { - #ifdef THREAD_DEBUG -  werror(sprintf("Handle thread [%O] calling %O(@%O)...\n", +  THREAD_WERROR(sprintf("Handle thread [%O] calling %O(@%O)...",    id, h[0], h[1..])); - #endif /* THREAD_DEBUG */ +     SET_LOCALE(default_locale);    h[0](@h[1]);    h=0;    } else if(!h) {    // Roxen is shutting down. -  werror("Handle thread ["+id+"] stopped\n"); +  report_debug("Handle thread ["+id+"] stopped\n");    thread_reap_cnt--;    return;    }
586: Inside #if defined(THREADS)
  void stop_handler_threads()   {    int timeout=10; -  roxen_perror("Stopping all request handler threads.\n"); +  report_debug("Stopping all request handler threads.\n");    while(number_of_threads>0) {    number_of_threads--;    handle_queue->write(0);
594: Inside #if defined(THREADS)
   }    while(thread_reap_cnt) {    if(--timeout<=0) { -  roxen_perror("Giving up waiting on threads!\n"); +  report_debug("Giving up waiting on threads!\n");    return;    }    sleep(0.1);
608:    string out;    string default_prefix;    int port; -  object f; +  Stdio.File f;       void die()    { - #ifdef SSL3_DEBUG -  roxen_perror(sprintf("SSL3:fallback_redirect_request::die()\n")); - #endif /* SSL3_DEBUG */ +  SSL3WERROR(sprintf("fallback_redirect_request::die()"));   #if 0    /* Close the file, DAMMIT */ -  object dummy = Stdio.File(); +  Stdio.File dummy = Stdio.File();    if (dummy->open("/dev/null", "rw"))    dummy->dup2(f);   #endif
628:       void write_callback(object id)    { - #ifdef SSL3_DEBUG -  roxen_perror(sprintf("SSL3:fallback_redirect_request::write_callback()\n")); - #endif /* SSL3_DEBUG */ +  SSL3WERROR(sprintf("fallback_redirect_request::write_callback()"));    int written = id->write(out);    if (written <= 0)    die();
641:       void read_callback(object id, string s)    { - #ifdef SSL3_DEBUG -  roxen_perror(sprintf("SSL3:fallback_redirect_request::read_callback(X, \"%s\")\n", s)); - #endif /* SSL3_DEBUG */ +  SSL3WERROR(sprintf("fallback_redirect_request::read_callback(X, \"%s\")\n", s));    in += s;    string name;    string prefix;       if (search(in, "\r\n\r\n") >= 0)    { - // werror(sprintf("request = '%s'\n", in)); + // werror("request = '%s'\n", in);    array(string) lines = in / "\r\n";    array(string) req = replace(lines[0], "\t", " ") / " ";    if (sizeof(req) < 2)
702:       void create(object socket, string s, string l, int p)    { - #ifdef SSL3_DEBUG -  roxen_perror(sprintf("SSL3:fallback_redirect_request(X, \"%s\", \"%s\", %d)\n", s, l||"CONFIG PORT", p)); - #endif /* SSL3_DEBUG */ +  SSL3WERROR(sprintf("fallback_redirect_request(X, \"%s\", \"%s\", %d)", s, l||"CONFIG PORT", p));    f = socket;    default_prefix = l;    port = p;
924: Inside #if constant(Standards) && constant(Standards.PKCS.RSA) && constant(SSL.sslfile)
   destruct(privs);       if (!f) { -  report_error("ssl3: Reading cert-file failed!\n"); +  report_error("SSL3: Reading cert-file failed!\n");    destruct();    return;    }
944: Inside #if constant(Standards) && constant(Standards.PKCS.RSA) && constant(SSL.sslfile)
   if (query_option("ssl_key_file"))    {    if (!f2) { -  report_error("ssl3: Reading key-file failed!\n"); +  report_error("SSL3: Reading key-file failed!\n");    destruct();    return;    }
953: Inside #if constant(Standards) && constant(Standards.PKCS.RSA) && constant(SSL.sslfile)
      function r = Crypto.randomness.reasonably_random()->read;    - #ifdef SSL3_DEBUG -  werror(sprintf("ssl3: key file contains: %O\n", indices(msg->parts))); - #endif +  SSL3WERROR(sprintf("key file contains: %O", indices(msg->parts)));       if (part = msg->parts["RSA PRIVATE KEY"])    {    string key;       if (!(key = part->decoded_body())) { -  report_error("ssl3: Private rsa key not valid (PEM).\n"); +  report_error("SSL3: Private rsa key not valid (PEM).\n");    destruct();    return;    }       object rsa = Standards.PKCS.RSA.parse_private_key(key);    if (!rsa) { -  report_error("ssl3: Private rsa key not valid (DER).\n"); +  report_error("SSL3: Private rsa key not valid (DER).\n");    destruct();    return;    }       ctx->rsa = rsa;    - #ifdef SSL3_DEBUG -  report_debug(sprintf("ssl3: RSA key size: %d bits\n", rsa->rsa_size())); - #endif +  SSL3WERROR(sprintf("RSA key size: %d bits", rsa->rsa_size()));       if (rsa->rsa_size() > 512)    {
1018: Inside #if constant(Standards) && constant(Standards.PKCS.RSA) && constant(SSL.sslfile)
   return;    }    - #ifdef SSL3_DEBUG -  report_debug(sprintf("ssl3: Using DSA key.\n")); - #endif +  SSL3WERROR(sprintf("Using DSA key."));       dsa->use_random(r);    ctx->dsa = dsa;
1301: Inside #if constant(SSL.sslfile)
      void ssl_alert_callback(object alert, object|int n, string data)    { - #ifdef SSL3_DEBUG -  roxen_perror(sprintf("SSL3:http_fallback(X, %O, \"%s\")\n", n, data)); - #endif /* SSL3_DEBUG */ +  SSL3WERROR(sprintf("http_fallback(X, %O, \"%s\")", n, data));    // trace(1);   #if 0    werror(sprintf("ssl3->http_fallback: alert(%d, %d)\n"
1332: Inside #if constant(SSL.sslfile)
      void ssl_accept_callback(object id)    { - #ifdef SSL3_DEBUG -  roxen_perror(sprintf("SSL3:ssl_accept_callback(X)\n")); - #endif /* SSL3_DEBUG */ +  SSL3WERROR(sprintf("ssl_accept_callback(X)"));    id->set_alert_callback(0); /* Forget about http_fallback */    my_fd = 0; /* Not needed any more */    }
1636:    }       if(type >= 1) -  werror( s ); +  report_debug( s );   }      // When was Roxen started?
1648:    return QUERY(default_ident)?real_version:QUERY(ident);   }    - public void log(mapping file, object request_id) + public void log(mapping file, RequestID request_id)   {    if(!request_id->conf) return;    request_id->conf->log(file, request_id);
1669:    }    current_user_id_number = (int)current_user_id_file->read(100);    current_user_id_file_last_mod = current_user_id_file->stat()[2]; -  perror("Restoring unique user ID information. (" + current_user_id_number +  report_debug("Restoring unique user ID information. (" + current_user_id_number    + ")\n");   #ifdef FD_DEBUG    mark_fd(current_user_id_file->query_fd(), LOCALE->unique_uid_logfile());
1687:    if(current_user_id_file->stat()[2] != current_user_id_file_last_mod)    restore_current_user_id_number();    current_user_id_number++; -  //perror("New unique id: "+current_user_id_number+"\n"); +  //werror("New unique id: "+current_user_id_number+"\n");    current_user_id_file->seek(0);    current_user_id_file->write((string)current_user_id_number);    current_user_id_file_last_mod = current_user_id_file->stat()[2];
1750:    if(!abs_started)    {    abs_started = 1; -  roxen_perror("Anti-Block System Enabled.\n"); +  report_debug("Anti-Block System Enabled.\n");    }    call_out (restart_if_stuck,10);    signal(signum("SIGALRM"),    lambda( int n ) { -  werror(sprintf("**** %s: ABS engaged!\n" +  report_debug("**** %s: ABS engaged!\n"    "Trying to dump backlog: \n", -  ctime(time()) - "\n")); +  ctime(time()) - "\n");    catch {    // Catch for paranoia reasons.    describe_all_threads();    }; -  werror(sprintf("**** %s: ABS exiting roxen!\n\n", -  ctime(time()))); +  report_debug("**** %s: ABS exiting roxen!\n\n", +  ctime(time()));    _exit(1); // It might now quit correctly otherwise, if it's    // locked up    });
1908:    ov = 255;    if( alpha )    { -  object i = Image.image( reply->xsize(), reply->ysize(), ov,ov,ov ); +  Image.image i = Image.image( reply->xsize(), reply->ysize(), ov,ov,ov );    i = i->paste_alpha( alpha, ov );    alpha = i;    }
1993:    case "gif":    if( alpha && true_alpha )    { -  object bw=Image.Colortable( ({ ({ 0,0,0 }), ({ 255,255,255 }) }) ); +  Image.Colortable bw=Image.Colortable( ({ ({ 0,0,0 }), ({ 255,255,255 }) }) );    bw->floyd_steinberg();    alpha = bw->map( alpha );    }
2235:    path += "/";    path += replace(name, "/", "_")+"/";    mkdirhier( path + "/tmp" ); -  object test = Stdio.File(); +  Stdio.File test = Stdio.File();    if (!test->open (path + "/.testfile", "wc"))    error ("Can't create files in the argument cache directory " + path + "\n");    else {
2291:    if( strlen(short_key) >= strlen(_key) )    short_key += "."; // Not very likely...    } -  object f = Stdio.File( path + short_key, "wct" ); +  Stdio.File f = Stdio.File( path + short_key, "wct" );    f->write( long_key );    return short_key;    }
2890:       if( tocolor && i && a )    { -  object o = Image.image( i->xsize(), i->ysize(), @tocolor ); +  Image.image o = Image.image( i->xsize(), i->ysize(), @tocolor );    o->paste_mask( i,a );    i = o;    }
2902:    ]);   }    - mapping low_load_image(string f,object id) + mapping low_load_image(string f, RequestID id)   {    string data; -  object file, img; +  Stdio.File file;    if(id->misc->_load_image_called < 5)    {    // We were recursing very badly with the demo module here...
2924:          - object load_image(string f,object id) + Image.image load_image(string f, RequestID id)   {    mapping q = low_load_image( f, id );    if( q ) return q->img;
2940: Inside #if undefined(__NT__)
  #ifndef __NT__    if(getuid())    { -  perror("It is impossible to chroot() if the server is not run as root.\n"); +  report_debug("It is impossible to chroot() if the server is not run as root.\n");    return;    }       if(!chroot(to))    { -  perror("Roxen: Cannot chroot to "+to+": "); +  report_debug("Roxen: Cannot chroot to "+to+": ");   #if efun(real_perror)    real_perror();   #endif    return;    } -  perror("Root is now "+to+".\n"); +  report_debug("Root is now "+to+".\n");   #endif   }   
2965: Inside #if undefined(__NT__)
      rm(where);    if(catch(Stdio.write_file(where, sprintf("%d\n%d", getpid(), getppid())))) -  perror("I cannot create the pid file ("+where+").\n"); +  report_debug("I cannot create the pid file ("+where+").\n");   #endif   }   
3002:   // FIXME: Ought to use the shutdown code.   void exit_when_done()   { -  object o; -  int i; -  roxen_perror("Interrupt request received. Exiting,\n"); +  report_debug("Interrupt request received. Exiting,\n");    die_die_die=1;       if(++_recurse > 4)    { -  roxen_perror("Exiting roxen (spurious signals received).\n"); +  report_debug("Exiting roxen (spurious signals received).\n");    configurations->stop();   #ifdef THREADS    stop_handler_threads();
3017:    exit(-1); // Restart.    }    -  roxen_perror("Exiting roxen.\n"); +  report_debug("Exiting roxen.\n");    configurations->stop();   #ifdef THREADS    stop_handler_threads();
3027:      void exit_it()   { -  perror("Recursive signals.\n"); +  report_debug("Recursive signals.\n");    exit(-1); // Restart.   }   
3049:    all_backtraces = ({ backtrace() });   #endif /* constant(all_threads) */    -  werror("Describing all threads:\n"); +  report_debug("Describing all threads:\n");    int i;    for(i=0; i < sizeof(all_backtraces); i++) { -  werror(sprintf("Thread %d:\n" +  report_debug("Thread %d:\n"    "%s\n",    i+1, -  describe_backtrace(all_backtraces[i]))); +  describe_backtrace(all_backtraces[i]));    }   }   
3071:    if(!p)    {   #ifdef DUMP_DEBUG -  report_debug(file+" not loaded, and thus cannot be dumped.\n"); +  werror(file+" not loaded, and thus cannot be dumped.\n");   #endif    return;    }
3084: Inside #if defined(DUMP_DEBUG)
   report_debug("** Cannot encode "+file+": "+describe_backtrace(q)+"\n");   #ifdef DUMP_DEBUG    else -  report_debug( file+" dumped successfully to "+file+".o\n" ); +  werror( file+" dumped successfully to "+file+".o\n" );   #endif    }   #ifdef DUMP_DEBUG    else -  report_debug(file+" already dumped (and up to date)\n"); +  werror(file+" already dumped (and up to date)\n");   #endif   }   
3170:    create_pid_file(Getopt.find_option(argv, "p", "pid-file", "ROXEN_PID_FILE")    || QUERY(pidfile));    -  roxen_perror("Initiating argument cache ... "); +  report_debug("Initiating argument cache ... ");       int id;    string cp = QUERY(argument_cache_dir), na = "args";
3186:    {    report_error( "Failed to initialize the global argument cache:\n"    + (describe_backtrace( e )/"\n")[0]+"\n"); -  werror( describe_backtrace( e ) ); +  report_debug( describe_backtrace( e ) );    } -  roxen_perror( "\n" ); +  report_debug( "\n" );       enable_configurations_modules();