예제 #1
0
int main(int argc, char **argv)
{
    struct sysgetenv sysgetenv;
    int i;
    int ex= 0;
    char *e;
    char val[1024];

    i= 1;
    while (i < argc && argv[i][0] == '-') {
	char *opt= argv[i++]+1;

	if (opt[0] == '-' && opt[1] == 0) break;	/* -- */

	if (*opt != 0) {
	    tell(2, "Usage: sysenv [name ...]\n", NIL);
	    exit(1);
	}
    }

    do {
	if (i < argc) {
	    sysgetenv.key= argv[i];
	    sysgetenv.keylen= strlen(sysgetenv.key) + 1;
	} else {
	    sysgetenv.key= nil;
	    sysgetenv.keylen= 0;
	}
	sysgetenv.val= val;
	sysgetenv.vallen= sizeof(val);

	if (svrctl(MMGETPARAM, &sysgetenv) == -1) {
	    if (errno == ESRCH) {
		ex |= 2;
	    } else {
		ex |= 1;
		tell(2, "sysenv: ", strerror(errno), "\n", NIL);
	    }
	    continue;
	}

	e= sysgetenv.val;
	do {
	    e += strlen(e);
	    *e++ = '\n';
	} while (i == argc && *e != 0);

	if (write(1, sysgetenv.val, e - sysgetenv.val) < 0) {
	    ex |= 1;
	    tell(2, "sysenv: ", strerror(errno), "\n", NIL);
	}
    } while (++i < argc);
    return ex;
}
예제 #2
0
파일: inet.c 프로젝트: EchoLiao/minix-study
PUBLIC void main()
{
	mq_t *mq;
	int r;
	int source, timerand, fd;
	struct fssignon device;
#ifdef __minix_vmd
	struct systaskinfo info;
#endif
	u8_t randbits[32];
	struct timeval tv;

#if DEBUG
	printf("Starting inet...\n");
	printf("%s\n", version);
#endif

	/* Read configuration. */
	nw_conf();

	/* Get a random number */
	timerand= 1;
	fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK);
	if (fd != -1)
	{
		r= read(fd, randbits, sizeof(randbits));
		if (r == sizeof(randbits))
			timerand= 0;
		else
		{
			printf("unable to read random data from %s: %s\n",
				RANDOM_DEV_NAME, r == -1 ? strerror(errno) :
				r == 0 ? "EOF" : "not enough data");
		}
		close(fd);
	}
	else
	{
		printf("unable to open random device %s: %s\n",
			RANDOM_DEV_NAME, strerror(errno));
	}
	if (timerand)
	{
		printf("using current time for random-number seed\n");
#ifdef __minix_vmd
		r= sysutime(UTIME_TIMEOFDAY, &tv);
#else /* Minix 3 */
		r= gettimeofday(&tv, NULL);
#endif
		if (r == -1)
		{
			printf("sysutime failed: %s\n", strerror(errno));
			exit(1);
		}
		memcpy(randbits, &tv, sizeof(tv));
	}
	init_rand256(randbits);

#ifdef __minix_vmd
	if (svrctl(SYSSIGNON, (void *) &info) == -1) pause();

	/* Our new identity as a server. */
	this_proc = info.proc_nr;
#else /* Minix 3 */

	/* Our new identity as a server. */
	if ((this_proc = getprocnr()) < 0)
		ip_panic(( "unable to get own process nr\n"));
#endif

	/* Register the device group. */
	device.dev= ip_dev;
	device.style= STYLE_CLONE;
	if (svrctl(FSSIGNON, (void *) &device) == -1) {
		printf("inet: error %d on registering ethernet devices\n",
			errno);
		pause();
	}

#ifdef BUF_CONSISTENCY_CHECK
	inet_buf_debug= (getenv("inetbufdebug") && 
		(strcmp(getenv("inetbufdebug"), "on") == 0));
	inet_buf_debug= 100;
	if (inet_buf_debug)
	{
		ip_warning(( "buffer consistency check enabled" ));
	}
#endif

	if (getenv("killerinet"))
	{
		ip_warning(( "killer inet active" ));
		killer_inet= 1;
	}

#ifdef __minix_vmd
	r= sys_findproc(SYN_AL_NAME, &synal_tasknr, 0);
	if (r != OK)
		ip_panic(( "unable to find synchronous alarm task: %d\n", r ));
#endif

	nw_init();
	while (TRUE)
	{
#ifdef BUF_CONSISTENCY_CHECK
		if (inet_buf_debug)
		{
			static int buf_debug_count= 0;

			if (++buf_debug_count >= inet_buf_debug)
			{
				buf_debug_count= 0;
				if (!bf_consistency_check())
					break;
			}
		}
#endif
		if (ev_head)
		{
			ev_process();
			continue;
		}
		if (clck_call_expire)
		{
			clck_expire_timers();
			continue;
		}
		mq= mq_get();
		if (!mq)
			ip_panic(("out of messages"));

		r= receive (ANY, &mq->mq_mess);
		if (r<0)
		{
			ip_panic(("unable to receive: %d", r));
		}
		reset_time();
		source= mq->mq_mess.m_source;
		if (source == FS_PROC_NR)
		{
			sr_rec(mq);
		}
#ifdef __minix_vmd
		else if (source == synal_tasknr)
		{
			clck_tick (&mq->mq_mess);
			mq_free(mq);
		}
#else /* Minix 3 */
		else if (mq->mq_mess.m_type == SYN_ALARM)
		{
			clck_tick(&mq->mq_mess);
			mq_free(mq);
		} 
		else if (mq->mq_mess.m_type == SYS_SIG)
		{
			/* signaled */ 
			/* probably SIGTERM */
			mq_free(mq);
		} 
		else if (mq->mq_mess.m_type & NOTIFY_MESSAGE)
		{
			/* A driver is (re)started. */
			eth_check_drivers(&mq->mq_mess);
			mq_free(mq);
		}
#endif
		else
		{
compare(mq->mq_mess.m_type, ==, DL_TASK_REPLY);
			eth_rec(&mq->mq_mess);
			mq_free(mq);
		}
	}
	ip_panic(("task is not allowed to terminate"));
}
예제 #3
0
파일: inet.c 프로젝트: locosoft1986/nucleos
void main(void)
{
	mq_t *mq;
	int r;
	int source, m_type, timerand, fd;
	u32_t tasknr;
	struct fssignon device;
	u8_t randbits[32];
	struct timeval tv;

#if DEBUG
	printk("Starting inet...\n");
	printk("%s\n", version);
#endif

#if HZ_DYNAMIC
	system_hz = sys_hz();
#endif

	/* Read configuration. */
	nw_conf();

	/* Get a random number */
	timerand= 1;
	fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK);
	if (fd != -1)
	{
		r= read(fd, randbits, sizeof(randbits));
		if (r == sizeof(randbits))
			timerand= 0;
		else
		{
			printk("inet: unable to read random data from %s: %s\n",
				RANDOM_DEV_NAME, r == -1 ? strerror(errno) :
				r == 0 ? "EOF" : "not enough data");
		}
		close(fd);
	}
	else
	{
		printk("inet: unable to open random device %s: %s\n",
			RANDOM_DEV_NAME, strerror(errno));
	}
	if (timerand)
	{
		printk("inet: using current time for random-number seed\n");
		r= gettimeofday(&tv, NULL);
		if (r == -1)
		{
			printk("sysutime failed: %s\n", strerror(errno));
			exit(1);
		}
		memcpy(randbits, &tv, sizeof(tv));
	}
	init_rand256(randbits);

	/* Our new identity as a server. */
	r= ds_retrieve_u32("inet", &tasknr);
	if (r != 0)
		ip_panic(("inet: ds_retrieve_u32 failed for 'inet': %d", r));
	this_proc= tasknr;

	/* Register the device group. */
	device.dev= ip_dev;
	device.style= STYLE_CLONE;
	if (svrctl(FSSIGNON, (void *) &device) == -1) {
		printk("inet: error %d on registering ethernet devices\n",
			errno);
		pause();
	}

#ifdef BUF_CONSISTENCY_CHECK
	inet_buf_debug= (getenv("inetbufdebug") && 
		(strcmp(getenv("inetbufdebug"), "on") == 0));
	inet_buf_debug= 100;
	if (inet_buf_debug)
	{
		ip_warning(( "buffer consistency check enabled" ));
	}
#endif

	if (getenv("killerinet"))
	{
		ip_warning(( "killer inet active" ));
		killer_inet= 1;
	}

	nw_init();
	while (TRUE)
	{
#ifdef BUF_CONSISTENCY_CHECK
		if (inet_buf_debug)
		{
			static int buf_debug_count= 0;

			if (++buf_debug_count >= inet_buf_debug)
			{
				buf_debug_count= 0;
				if (!bf_consistency_check())
					break;
			}
		}
#endif
		if (ev_head)
		{
			ev_process();
			continue;
		}
		if (clck_call_expire)
		{
			clck_expire_timers();
			continue;
		}
		mq= mq_get();
		if (!mq)
			ip_panic(("out of messages"));

		r = kipc_module_call(KIPC_RECEIVE, 0, ENDPT_ANY, &mq->mq_mess);
		if (r<0)
		{
			ip_panic(("unable to receive: %d", r));
		}
		reset_time();
		source= mq->mq_mess.m_source;
		m_type= mq->mq_mess.m_type;

		if (source == VFS_PROC_NR) {
			sr_rec(mq);
		} else if (is_notify(m_type)) {
			if (_ENDPOINT_P(source) == CLOCK) {
				clck_tick(&mq->mq_mess);
				mq_free(mq);
			} else if (_ENDPOINT_P(source) == PM_PROC_NR) {
				/* signaled */
				/* probably SIGTERM */
				mq_free(mq);
			} else {
				/* A driver is (re)started. */
				eth_check_drivers(&mq->mq_mess);
				mq_free(mq);
			}
		} else if (m_type == DL_CONF_REPLY || m_type == DL_TASK_REPLY ||
			m_type == DL_NAME_REPLY || m_type == DL_STAT_REPLY) {
			eth_rec(&mq->mq_mess);
			mq_free(mq);
		} else {
			printk("inet: got bad message type 0x%x from %d\n",
				mq->mq_mess.m_type, mq->mq_mess.m_source);
			mq_free(mq);
		}
	}
	ip_panic(("task is not allowed to terminate"));
}
예제 #4
0
파일: init.c 프로젝트: Ga-vin/MINIX3
int main(void)
{
  pid_t pid;			/* pid of child process */
  int fd;			/* generally useful */
  int linenr;			/* loop variable */
  int check;			/* check if a new process must be spawned */
  int sn;			/* signal number */
  struct slotent *slotp;	/* slots[] pointer */
  struct ttyent *ttyp;		/* ttytab entry */
  struct sigaction sa;
  struct stat stb;

#define OPENFDS						\
  if (fstat(0, &stb) < 0) {				\
	/* Open standard input, output & error. */	\
	(void) open("/dev/null", O_RDONLY);		\
	(void) open("/dev/log", O_WRONLY);		\
	dup(1);						\
  }

  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;

  /* Default: Ignore every signal (except those that follow). */
  sa.sa_handler = SIG_IGN;
  for (sn = 1; sn < _NSIG; sn++) {
      sigaction(sn, &sa, NULL);
  }

  /* Hangup: Reexamine /etc/ttytab for newly enabled terminal lines. */
  sa.sa_handler = onhup;
  sigaction(SIGHUP, &sa, NULL);

  /* Terminate: Stop spawning login processes, shutdown is near. */
  sa.sa_handler = onterm;
  sigaction(SIGTERM, &sa, NULL);

  /* Abort: Sent by the kernel on CTRL-ALT-DEL; shut the system down. */
  sa.sa_handler = onabrt;
  sigaction(SIGABRT, &sa, NULL);

  /* Execute the /etc/rc file. */
  if ((pid = fork()) != 0) {
	/* Parent just waits. */
	while (wait(NULL) != pid) {
		if (gotabrt) reboot(RBT_HALT);
	}
  } else {
#if ! SYS_GETKENV
	struct sysgetenv sysgetenv;
#endif
	char bootopts[16];
	static char *rc_command[] = { "sh", "/etc/rc", NULL, NULL, NULL };
	char **rcp = rc_command + 2;

	/* Get the boot options from the boot environment. */
	sysgetenv.key = "bootopts";
	sysgetenv.keylen = 8+1;
	sysgetenv.val = bootopts;
	sysgetenv.vallen = sizeof(bootopts);
	if (svrctl(PMGETPARAM, &sysgetenv) == 0) *rcp++ = bootopts;
	*rcp = "start";

	execute(rc_command);
	report(2, "sh /etc/rc");
	_exit(1);	/* impossible, we hope */
  }

  OPENFDS;

  /* Clear /etc/utmp if it exists. */
  if ((fd = open(PATH_UTMP, O_WRONLY | O_TRUNC)) >= 0) close(fd);

  /* Log system reboot. */
  wtmp(BOOT_TIME, 0, NULL, 0);

  /* Main loop. If login processes have already been started up, wait for one
   * to terminate, or for a HUP signal to arrive. Start up new login processes
   * for all ttys which don't have them. Note that wait() also returns when
   * somebody's orphan dies, in which case ignore it.  If the TERM signal is
   * sent then stop spawning processes, shutdown time is near.
   */

  check = 1;
  while (1) {
	while ((pid = waitpid(-1, NULL, check ? WNOHANG : 0)) > 0) {
		/* Search to see which line terminated. */
		for (linenr = 0; linenr < PIDSLOTS; linenr++) {
			slotp = &slots[linenr];
			if (slotp->pid == pid) {
				/* Record process exiting. */
				wtmp(DEAD_PROCESS, linenr, NULL, pid);
				slotp->pid = NO_PID;
				check = 1;
			}
		}
	}

	/* If a signal 1 (SIGHUP) is received, simply reset error counts. */
	if (gothup) {
		gothup = 0;
		for (linenr = 0; linenr < PIDSLOTS; linenr++) {
			slots[linenr].errct = 0;
		}
		check = 1;
	}

	/* Shut down on signal 6 (SIGABRT). */
	if (gotabrt) {
		gotabrt = 0;
		startup(0, &TT_REBOOT);
	}

	if (spawn && check) {
		/* See which lines need a login process started up. */
		for (linenr = 0; linenr < PIDSLOTS; linenr++) {
			slotp = &slots[linenr];
			if ((ttyp = getttyent()) == NULL) break;

			if (ttyp->ty_getty != NULL
				/* ty_getty is a string, and TTY_ON is
				 * the way to check for enabled ternimanls. */
				&& (ttyp->ty_status & TTY_ON)
				&& slotp->pid == NO_PID
				&& slotp->errct < ERRCT_DISABLE)
			{
				startup(linenr, ttyp);
			}
		}
		endttyent();
	}
	check = 0;
  }
}
예제 #5
0
int main(int argc, char **argv)
{
  struct tm time1;
  struct tm time2;
  struct tm tmnow;
  char date[64];
  time_t now, rtc;
  int i, mem;
  unsigned char mach_id, cmos_state;
  struct sysgetenv sysgetenv;

  /* Open /dev/mem to get access to physical memory. */
  if ((mem = open("/dev/mem", O_RDONLY)) == -1) {
	errmsg( "Permission denied." );
	exit(1);
  }
  if (lseek(mem, (off_t) MACH_ID_ADDR, SEEK_SET) == -1
		|| read(mem, (void *) &mach_id, sizeof(mach_id)) < 0) {
	mach_id = -1;
  }
  if (mach_id != PS_386 && mach_id != PC_AT) {
	errmsg( "Machine ID unknown." );
	fprintf( stderr, "Machine ID byte = %02x\n", mach_id );

	exit(1);
  }
  cmos_state = read_register(CMOS_STATUS);
  if (cmos_state & (CS_LOST_POWER | CS_BAD_CHKSUM | CS_BAD_TIME)) {
	errmsg( "CMOS RAM error(s) found..." );
	fprintf( stderr, "CMOS state = 0x%02x\n", cmos_state );

	if (cmos_state & CS_LOST_POWER)
	    errmsg( "RTC lost power. Reset CMOS RAM with SETUP." );
	if (cmos_state & CS_BAD_CHKSUM)
	    errmsg( "CMOS RAM checksum is bad. Run SETUP." );
	if (cmos_state & CS_BAD_TIME)
	    errmsg( "Time invalid in CMOS RAM. Reset clock." );
	exit(1);
  }

  /* Process options. */
  while (argc > 1) {
	char *p = *++argv;

	if (*p++ != '-') usage();

	while (*p != 0) {
		switch (*p++) {
		case 'n':	nflag = 1;	break;
		case 'w':	wflag = 1;	break;
		case 'W':	Wflag = 1;	break;
		case '2':	y2kflag = 1;	break;
		default:	usage();
		}
	}
	argc--;
  }
  if (Wflag) wflag = 1;		/* -W implies -w */

  /* The hardware clock may run in a different time zone, likely GMT or
   * winter time.  Select that time zone.
   */
  strcpy(clocktz, "TZ=");
  sysgetenv.key = "TZ";
  sysgetenv.keylen = 2+1;
  sysgetenv.val = clocktz+3;
  sysgetenv.vallen = sizeof(clocktz)-3;
  if (svrctl(SYSGETENV, &sysgetenv) == 0) {
	putenv(clocktz);
	tzset();
  }

  /* Read the CMOS real time clock. */
  for (i = 0; i < 10; i++) {
	get_time(&time1);
	now = time(NULL);

	time1.tm_isdst = -1;	/* Do timezone calculations. */
	time2 = time1;

	rtc= mktime(&time1);	/* Transform to a time_t. */
	if (rtc != -1) break;

	fprintf(stderr,
"readclock: Invalid time read from CMOS RTC: %d-%02d-%02d %02d:%02d:%02d\n",
		time2.tm_year+1900, time2.tm_mon+1, time2.tm_mday,
		time2.tm_hour, time2.tm_min, time2.tm_sec);
	sleep(5);
  }
  if (i == 10) exit(1);

  if (!wflag) {
	/* Set system time. */
	if (nflag) {
		printf("stime(%lu)\n", (unsigned long) rtc);
	} else {
		if (stime(&rtc) < 0) {
			errmsg( "Not allowed to set time." );
			exit(1);
		}
	}
	tmnow = *localtime(&rtc);
	if (strftime(date, sizeof(date),
				"%a %b %d %H:%M:%S %Z %Y", &tmnow) != 0) {
		if (date[8] == '0') date[8]= ' ';
		printf("Result: %s\n", date);
	}
  } else {
	/* Set the CMOS clock to the system time. */
	tmnow = *localtime(&now);
	if (nflag) {
		printf("%04d-%02d-%02d %02d:%02d:%02d\n",
			tmnow.tm_year + 1900,
			tmnow.tm_mon + 1,
			tmnow.tm_mday,
			tmnow.tm_hour,
			tmnow.tm_min,
			tmnow.tm_sec);
	} else {
		set_time(&tmnow);
	}
  }
  exit(0);
}