home *** CD-ROM | disk | FTP | other *** search
- /*
- m0.c
- */
- /* Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
-
- Distributed under the terms of the GNU General Public License
- version 2 of june 1991 as published by the Free Software
- Foundation, Inc.
-
- This file is part of M0.
-
- M0 is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY. No author or distributor accepts responsibility to anyone for
- the consequences of using it or for whether it serves any particular
- purpose or works at all, unless he says so in writing. Refer to the GNU
- General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute M0, but
- only under the conditions described in the GNU General Public License.
- A copy of this license is supposed to have been given to you along with
- M0 so you can know your rights and responsibilities. It should be in a
- file named LICENSE. Among other things, the copyright notice and this
- notice must be preserved on all copies. */
-
- #include <stdlib.h>
- #include <signal.h>
-
- #include "l_proto.h"
- #include "c_proto.h"
- #include "copyrght.h"
-
-
- #define VERSION "0.11"
-
- static statline = 0;
- static void usage(FILE *f);
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- ushort port = UDP_SERVERPORT;
-
- while (argc > 1 && argv[1][0] == '-') {
- argc--, argv++;
- switch(argv[0][1]) {
- case 'h':
- usage(stdout); break;
- case 's':
- statline = !statline; break;
- #ifdef DEBUG
- case 't':
- trace = atoi(argv[0]+2); break;
- #endif
- case 'u':
- if (argc < 2) usage(stderr);
- port = strtol(argv[1], NULL, 0);
- argc--; argv++; break;
- case 'v':
- printf("The M0 platform, version %s (compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- exit(0);
- default:
- usage(stderr);
- }
- }
-
- printf("## Welcome to the M0 platform (v%s, compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- printf("## %s\n\n", COPYRIGHT);
-
- if (init(port)) {
- fprintf(stderr, "M0 initialisation failed\n");
- exit(1);
- }
-
- server_loop();
-
- exit(0);
- }
-
- /* ---------------------------------------------------------------------- */
-
-
- init(int port)
- {
- char *bin, *lib;
- byte h[8];
-
- if ( low_level_init() != OK || operator_init() != OK) {
- fprintf(stderr, "low level initialization error\n");
- exit(1);
- }
-
- #ifdef CHANNEL_UDP
- if (udp_init(get_ip_addresses(), port) != 0)
- fprintf(stderr, "## Cannot find an empty UDP port\n");
- else {
- int i;
- printf("## Listening on UDP port 0x%04x at IP address(es)", port);
- for (i = 0; i < eplen(gaddr(udp_addr)); i++) {
- eindex e = array_get(0, array_get(0, udp_addr, i), 0);
- printf(" %s (=%u)", iptoa(gaddr(e)->V.i), gaddr(e)->V.i);
- }
- printf("\n");
- }
- #endif
-
- #ifdef CHANNEL_NIT
- if (nit_init(get_eth_devices(), ETHERNET_TYPE) != 0)
- fprintf(stderr, "## Cannot open ethernet device(s)\n");
- else {
- int i;
- printf("## Listening for 0x%04x ethernet frames on", ETHERNET_TYPE);
- for (i = 0; i < eplen(gaddr(nit_addr)); i++) {
- eindex e = array_get(0, nit_addr, i);
- byte s[6];
- str_export(0, s, e, 0, 6);
- printf(" %02x:%02x:%02x:%02x:%02x:%02x",
- s[0], s[1], s[2], s[3], s[4], s[5]);
- }
- printf("\n");
- }
- #endif
-
- if (channel_defs() != OK) {
- fprintf(stderr, "channel level initialization error\n");
- exit(1);
- }
-
- #ifdef SUNOS5
- setuid(getuid());
- seteuid(getuid());
- #else
- # ifndef __MSDOS__
- setreuid(getuid(), getuid());
- # endif
- #endif
-
- bin = getenv("M0BIN");
- lib = getenv("M0LIB");
- if( init_interpreter(bin?bin:DEFBINPATH, lib?lib:DEFLIBPATH) != OK)
- return 1;
-
- signal(SIGHUP, terminate);
- signal(SIGINT, terminate);
- signal(SIGTERM, terminate);
-
- fillin_hostid(h);
- printf("## This is M0 host \\%02x%02x%02x%02x%02x%02x%02x%02x\\\n",
- h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]);
-
- return 0;
- }
-
-
- server_loop()
- {
- long to;
-
- for (;;) {
- for (;;) {
- if (runable())
- run();
- to = next_timeout();
- if (to == 0)
- timeout(time_queue);
- if (!incoming_wouldblock()) {
- to = 0;
- break;
- }
- if (to != 0 && !runable()) {
- remove_refcycles();
- break;
- }
- }
- if (statline)
- statusline();
- serve_incoming(to);
- }
- }
-
-
- void usage(FILE *f)
- {
- fprintf(f, "The M0 platform, version %s (compiled %s at %s)\n",
- VERSION, __DATE__, __TIME__);
- fprintf(f, "%s\n", COPYRIGHT);
- fprintf(f, "usage: m0 [options]\n");
- fprintf(f, "options:\n"
- "\t-help\n"
- "\t-s <statusline_toggle>\n"
- #ifdef DEBUG
- "\t-t<trace_level>\n"
- #endif
- "\t-u <server_udp_port>\n"
- "\t-version\n");
- fprintf(f, "environment variables:\n"
- "\tM0BIN path for finding the m0strip binary\n"
- "\tM0LIB path for finding the M0 startup files\n");
- exit(f == stdout ? 0 : 1);
- }
-
-
- statusline()
- {
- uint a, b, t;
-
- get_proc_stats( &a, &b, &t);
- printf(" %d process(es): %d active, %d blocked, %d timeout \r",
- a+b+t, a, b, t);
- fflush(stdout);
- }
-