home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part03 / m0.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  4.7 KB  |  215 lines

  1. /*
  2.     m0.c
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #include <stdlib.h>
  26. #include <signal.h>
  27.  
  28. #include "l_proto.h"
  29. #include "c_proto.h"
  30. #include "copyrght.h"
  31.  
  32.  
  33. #define VERSION "0.11"
  34.  
  35. static statline = 0;
  36. static void usage(FILE *f);
  37.  
  38.  
  39. main(argc, argv)
  40. int argc;
  41. char *argv[];
  42. {
  43.     ushort port = UDP_SERVERPORT;
  44.  
  45.     while (argc > 1 && argv[1][0] == '-') {
  46.         argc--, argv++;
  47.         switch(argv[0][1]) {
  48.             case 'h':
  49.             usage(stdout); break;
  50.             case 's':
  51.             statline = !statline; break;
  52. #ifdef DEBUG
  53.             case 't':
  54.             trace = atoi(argv[0]+2); break;
  55. #endif
  56.             case 'u':
  57.             if (argc < 2) usage(stderr);
  58.             port = strtol(argv[1], NULL, 0);
  59.             argc--; argv++; break;
  60.             case 'v':
  61.             printf("The M0 platform, version %s (compiled %s at %s)\n",
  62.                 VERSION, __DATE__, __TIME__);
  63.             exit(0);
  64.             default:
  65.             usage(stderr);
  66.         }
  67.     }
  68.  
  69.     printf("## Welcome to the M0 platform (v%s, compiled %s at %s)\n",
  70.                  VERSION, __DATE__, __TIME__);
  71.     printf("## %s\n\n", COPYRIGHT);
  72.  
  73.     if (init(port)) {
  74.         fprintf(stderr, "M0 initialisation failed\n");
  75.         exit(1);
  76.     }
  77.  
  78.     server_loop();
  79.  
  80.     exit(0);
  81. }
  82.  
  83. /* ---------------------------------------------------------------------- */
  84.  
  85.  
  86. init(int port)
  87. {
  88.     char *bin, *lib;
  89.     byte h[8];
  90.  
  91.     if (   low_level_init() != OK || operator_init() != OK) {
  92.         fprintf(stderr, "low level initialization error\n");
  93.         exit(1);
  94.     }
  95.  
  96. #ifdef CHANNEL_UDP
  97.     if (udp_init(get_ip_addresses(), port) != 0)
  98.         fprintf(stderr, "## Cannot find an empty UDP port\n");
  99.     else {
  100.         int i;
  101.         printf("## Listening on UDP port 0x%04x at IP address(es)", port);
  102.         for (i = 0; i < eplen(gaddr(udp_addr)); i++) {
  103.             eindex e = array_get(0, array_get(0, udp_addr, i), 0);
  104.             printf(" %s (=%u)", iptoa(gaddr(e)->V.i), gaddr(e)->V.i);
  105.         }
  106.         printf("\n");
  107.     }
  108. #endif
  109.  
  110. #ifdef CHANNEL_NIT
  111.     if (nit_init(get_eth_devices(), ETHERNET_TYPE) != 0)
  112.         fprintf(stderr, "## Cannot open ethernet device(s)\n");
  113.     else {
  114.         int i;
  115.         printf("## Listening for 0x%04x ethernet frames on", ETHERNET_TYPE);
  116.         for (i = 0; i < eplen(gaddr(nit_addr)); i++) {
  117.             eindex e = array_get(0, nit_addr, i);
  118.             byte s[6];
  119.             str_export(0, s, e, 0, 6);
  120.             printf(" %02x:%02x:%02x:%02x:%02x:%02x",
  121.                 s[0], s[1], s[2], s[3], s[4], s[5]);
  122.         }
  123.         printf("\n");
  124.     }
  125. #endif
  126.  
  127.     if (channel_defs() != OK) {
  128.         fprintf(stderr, "channel level initialization error\n");
  129.         exit(1);
  130.     }
  131.  
  132. #ifdef SUNOS5
  133.     setuid(getuid());
  134.     seteuid(getuid());
  135. #else
  136. # ifndef __MSDOS__
  137.     setreuid(getuid(), getuid());
  138. # endif
  139. #endif
  140.  
  141.     bin = getenv("M0BIN");
  142.     lib = getenv("M0LIB");
  143.     if( init_interpreter(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH) != OK)
  144.         return 1;
  145.  
  146.     signal(SIGHUP, terminate);
  147.     signal(SIGINT, terminate);
  148.     signal(SIGTERM, terminate);
  149.  
  150.     fillin_hostid(h);
  151.     printf("## This is M0 host \\%02x%02x%02x%02x%02x%02x%02x%02x\\\n",
  152.             h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]);
  153.  
  154.     return 0;
  155. }
  156.  
  157.  
  158. server_loop()
  159. {
  160.     long to;
  161.  
  162.     for (;;) {
  163.         for (;;) {
  164.             if (runable())
  165.                 run();
  166.             to = next_timeout();
  167.             if (to == 0)
  168.                 timeout(time_queue);
  169.             if (!incoming_wouldblock()) {
  170.                 to = 0;
  171.                 break;
  172.             }
  173.             if (to != 0 && !runable()) {
  174.                 remove_refcycles();
  175.                 break;
  176.             }
  177.         }
  178.         if (statline)
  179.             statusline();
  180.         serve_incoming(to);
  181.     }
  182. }
  183.  
  184.  
  185. void usage(FILE *f)
  186. {
  187.     fprintf(f, "The M0 platform, version %s (compiled %s at %s)\n",
  188.                 VERSION, __DATE__, __TIME__);
  189.     fprintf(f, "%s\n", COPYRIGHT);
  190.     fprintf(f, "usage: m0 [options]\n");
  191.     fprintf(f, "options:\n"
  192.             "\t-help\n"
  193.             "\t-s <statusline_toggle>\n"
  194. #ifdef DEBUG
  195.             "\t-t<trace_level>\n"
  196. #endif
  197.             "\t-u <server_udp_port>\n"
  198.             "\t-version\n");
  199.     fprintf(f, "environment variables:\n"
  200.             "\tM0BIN  path for finding the m0strip binary\n"
  201.             "\tM0LIB  path for finding the M0 startup files\n");
  202.     exit(f == stdout ? 0 : 1);
  203. }
  204.  
  205.  
  206. statusline()
  207. {
  208.     uint a, b, t;
  209.  
  210.     get_proc_stats( &a, &b, &t);
  211.     printf("  %d process(es): %d active, %d blocked, %d timeout    \r",
  212.         a+b+t, a, b, t);
  213.     fflush(stdout);
  214. }
  215.