void md_init(void) { #if defined(LINUX) || defined(_ALLBSD_SOURCE) /* No Hi-Res timer option? */ #else if ( gdata->micro_state_accounting ) { char proc_ctl_fn[48]; int procfd; /* Turn on micro state accounting, once per process */ (void)md_snprintf(proc_ctl_fn, sizeof(proc_ctl_fn), "/proc/%d/ctl", md_getpid()); procfd = open(proc_ctl_fn, O_WRONLY); if (procfd >= 0) { long ctl_op[2]; ctl_op[0] = PCSET; ctl_op[1] = PR_MSACCT; (void)write(procfd, ctl_op, sizeof(ctl_op)); (void)close(procfd); } } #endif }
void error_do_pause(void) { int pid = md_getpid(); int timeleft = 600; /* 10 minutes max */ int interval = 10; /* 10 second message check */ /*LINTED*/ error_message("\nHPROF pause for PID %d\n", (int)pid); while ( p && timeleft > 0 ) { md_sleep(interval); /* 'assign p=0' to stop pause loop */ timeleft -= interval; } if ( timeleft <= 0 ) { error_message("\n HPROF pause got tired of waiting and gave up.\n"); } }
/* ARGSUSED */ static int notify_ioctl(dev_t dev, int icmd, void *ioctl_in, int mode, IOLOCK *lockp) { int cmd; pid_t pid; md_event_queue_t *event_queue; md_event_t *event; cred_t *credp; char *q_name; int err = 0; size_t sz = 0; md_event_ioctl_t *ioctl; sz = sizeof (*ioctl); ioctl = kmem_zalloc(sz, KM_SLEEP); if (ddi_copyin(ioctl_in, (void *)ioctl, sz, mode)) { err = EFAULT; goto out; } if (ioctl->mdn_rev != MD_NOTIFY_REVISION) { err = EINVAL; goto out; } if (ioctl->mdn_magic != MD_EVENT_ID) { err = EINVAL; goto out; } pid = md_getpid(); cmd = ioctl->mdn_cmd; q_name = ioctl->mdn_name; if (((cmd != EQ_OFF) && (cmd != EQ_ON)) && (md_reap >= md_reap_count)) md_reaper(); if ((cmd != EQ_ON) && (cmd != EQ_PUT)) { mutex_enter(&md_eventq_mx); if ((event_queue = md_find_event_queue(q_name, 0)) == NULL) { mutex_exit(&md_eventq_mx); (void) notify_fillin_empty_ioctl ((void *)ioctl, ioctl_in, sz, mode); err = ENOENT; goto out; } } switch (cmd) { case EQ_ON: md_reaper(); mutex_enter(&md_eventq_mx); if (md_find_event_queue(q_name, 0) != NULL) { mutex_exit(&md_eventq_mx); err = EEXIST; break; } /* allocate and initialize queue head */ event_queue = (md_event_queue_t *) kmem_alloc(sizeof (md_event_queue_t), KM_NOSLEEP); if (event_queue == NULL) { mutex_exit(&md_eventq_mx); err = ENOMEM; break; } cv_init(&event_queue->mdn_cv, NULL, CV_DEFAULT, NULL); event_queue->mdn_flags = 0; event_queue->mdn_pid = pid; event_queue->mdn_proc = md_getproc(); event_queue->mdn_size = 0; event_queue->mdn_front = NULL; event_queue->mdn_tail = NULL; event_queue->mdn_waiting = 0; event_queue->mdn_nextq = NULL; credp = ddi_get_cred(); event_queue->mdn_uid = crgetuid(credp); bcopy(q_name, event_queue->mdn_name, MD_NOTIFY_NAME_SIZE); if (ioctl->mdn_flags & EQ_Q_PERM) event_queue->mdn_flags |= MD_EVENT_QUEUE_PERM; /* link into the list of event queues */ if (md_event_queue != NULL) event_queue->mdn_nextq = md_event_queue; md_event_queue = event_queue; mutex_exit(&md_eventq_mx); err = 0; break; case EQ_OFF: if (md_event_queue == NULL) return (ENOENT); event_queue->mdn_flags = MD_EVENT_QUEUE_DESTROY; event_queue->mdn_pid = 0; event_queue->mdn_proc = NULL; if (event_queue->mdn_waiting != 0) cv_broadcast(&event_queue->mdn_cv); /* * force the reaper to delete this when it has no process * waiting on it. */ mutex_exit(&md_eventq_mx); md_reaper(); err = 0; break; case EQ_GET_NOWAIT: case EQ_GET_WAIT: if (cmd == EQ_GET_WAIT) { err = md_wait_for_event(event_queue, ioctl_in, ioctl, sz, mode, lockp); if (err == EINTR) goto out; } ASSERT(MUTEX_HELD(&md_eventq_mx)); if (event_queue->mdn_flags & (MD_EVENT_QUEUE_INVALID | MD_EVENT_QUEUE_FULL)) { event_queue->mdn_flags &= ~(MD_EVENT_QUEUE_INVALID | MD_EVENT_QUEUE_FULL); mutex_exit(&md_eventq_mx); err = notify_fillin_empty_ioctl ((void *)ioctl, ioctl_in, sz, mode); ioctl->mdn_event = EQ_NOTIFY_LOST; err = ddi_copyout((void *)ioctl, ioctl_in, sz, mode); if (err) err = EFAULT; goto out; } if (event_queue->mdn_front != NULL) { event = event_queue->mdn_front; event_queue->mdn_front = event->mdn_next; event_queue->mdn_size--; if (event_queue->mdn_front == NULL) event_queue->mdn_tail = NULL; mutex_exit(&md_eventq_mx); ioctl->mdn_tag = event->mdn_tag; ioctl->mdn_set = event->mdn_set; ioctl->mdn_dev = event->mdn_dev; ioctl->mdn_event = event->mdn_event; ioctl->mdn_user = event->mdn_user; ioctl->mdn_time.tv_sec = event->mdn_time.tv_sec; ioctl->mdn_time.tv_usec = event->mdn_time.tv_usec; kmem_free(event, sizeof (md_event_t)); err = ddi_copyout((void *)ioctl, ioctl_in, sz, mode); if (err) err = EFAULT; goto out; } else { /* no elements on queue */ mutex_exit(&md_eventq_mx); err = notify_fillin_empty_ioctl ((void *)ioctl, ioctl_in, sz, mode); if (err) err = EFAULT; } if (cmd == EQ_GET_NOWAIT) err = EAGAIN; goto out; case EQ_PUT: if (!md_event_queue) { err = ENOENT; break; } md_put_event(ioctl->mdn_tag, ioctl->mdn_set, ioctl->mdn_dev, ioctl->mdn_event, ioctl->mdn_user); err = 0; goto out; default: err = EINVAL; goto out; } out: kmem_free(ioctl, sz); return (err); }
/* * main: * The main program, of course */ int main(int argc, char **argv) { char *env; time_t lowtime; md_init(); #ifdef MASTER /* * Check to see if he is a wizard */ if (argc >= 2 && argv[1][0] == '\0') if (strcmp(PASSWD, md_crypt(md_getpass("wizard's password: "******"mT")) == 0) { wizard = TRUE; player.t_flags |= SEEMONST; argv++; argc--; } #endif /* * get home and options from environment */ strcpy(home, md_gethomedir()); if (strlen(home) > MAXSTR - strlen("rogue.save") - 1) *home = 0; strcpy(file_name, home); strcat(file_name, "rogue.save"); if ((env = getenv("ROGUEOPTS")) != NULL) parse_opts(env); if (env == NULL || whoami[0] == '\0') strucpy(whoami, md_getusername(), strlen(md_getusername())); lowtime = time(NULL); if (getenv("SEED") != NULL) { dnum = atoi(getenv("SEED")); noscore = 1; } else dnum = (unsigned int) lowtime + md_getpid(); seed = dnum; open_score(); /* * Drop setuid/setgid after opening the scoreboard file. */ md_normaluser(); /* * check for print-score option */ md_normaluser(); /* we drop any setgid/setuid priveldges here */ if (argc == 2) { if (strcmp(argv[1], "-s") == 0) { noscore = TRUE; score(0, -1, 0); exit(0); } else if (strcmp(argv[1], "-d") == 0) { dnum = rnd(100); /* throw away some rnd()s to break patterns */ while (--dnum) rnd(100); purse = rnd(100) + 1; level = rnd(100) + 1; initscr(); getltchars(); death(death_monst()); exit(0); } } init_check(); /* check for legal startup */ if (argc == 2) if (!restore(argv[1])) /* Note: restore will never return */ my_exit(1); #ifdef MASTER if (wizard) printf("Hello %s, welcome to dungeon #%d", whoami, dnum); else #endif printf("Hello %s, just a moment while I dig the dungeon...", whoami); fflush(stdout); initscr(); /* Start up cursor package */ init_probs(); /* Set up prob tables for objects */ init_player(); /* Set up initial player stats */ init_names(); /* Set up names of scrolls */ init_colors(); /* Set up colors of potions */ init_stones(); /* Set up stone settings of rings */ init_materials(); /* Set up materials of wands */ setup(); /* * The screen must be at least NUMLINES x NUMCOLS */ if (LINES < NUMLINES || COLS < NUMCOLS) { printf("\nSorry, the screen must be at least %dx%d\n", NUMLINES, NUMCOLS); endwin(); my_exit(1); } /* * Set up windows */ hw = newwin(LINES, COLS, 0, 0); idlok(stdscr, TRUE); idlok(hw, TRUE); #ifdef MASTER noscore = wizard; #endif new_level(); /* Draw current level */ /* * Start up daemons and fuses */ start_daemon(runners, 0, AFTER); start_daemon(doctor, 0, AFTER); fuse(swander, 0, WANDERTIME, AFTER); start_daemon(stomach, 0, AFTER); playit(); return(0); }
int restore(char *file, char **envp) { FILE *inf; extern char **environ; char buf[LINELEN]; STAT sbuf2; int oldcol, oldline; /* Old number of columns and lines */ if (strcmp(file, "-r") == 0) file = file_name; if ((inf = fopen(file, "r")) == NULL) { perror(file); return FALSE; } fflush(stdout); ENCREAD(buf, strlen(version) + 1, inf); if (strcmp(buf, version) != 0) { printf("Sorry, saved game is out of date.\n"); return FALSE; } /* * Get the lines and columns from the previous game */ ENCREAD(buf, 80, inf); sscanf(buf, "%d x %d\n", &oldline, &oldcol); stat(file, &sbuf2); fflush(stdout); /* * Set the new terminal and make sure we aren't going to a smaller screen. */ initscr(); if (MY_COLS < oldcol || MY_LINES < oldline) { printf("Cannot restart the game on a smaller screen.\n"); return FALSE; } cw = newwin(MY_LINES, MY_COLS, 0, 0); mw = newwin(MY_LINES, MY_COLS, 0, 0); hw = newwin(MY_LINES, MY_COLS, 0, 0); msgw = newwin(4, MY_COLS, 0, 0); keypad(cw,1); keypad(msgw,1); mpos = 0; mvwprintw(cw, 0, 0, "%s: %s", file, ctime(&sbuf2.st_mtime)); /* * defeat multiple restarting from the same place */ if (!wizard) { if (sbuf2.st_nlink != 1) { printf("Cannot restore from a linked file\n"); return FALSE; } } if (rs_restore_file(inf) != 0) { endwin(); printf("\nCannot restore file\n"); return(FALSE); } if (!wizard) { if (md_unlink(file) < 0) { fclose(inf); /* only close if system insists */ if (md_unlink(file) < 0) { endwin(); printf("\nCannot unlink file\n"); return FALSE; } } } environ = envp; strcpy(file_name, file); setup(); clearok(curscr, TRUE); touchwin(cw); srand(md_getpid()); playit(); return(FALSE); /*NOTREACHED*/ }