int main(int argc, char *argv[]) { static const char *const devfiles[] = { "/dev/snd/controlC", "/dev/snd/pcmC", "/dev/snd/midiC", "/dev/snd/hwC", NULL }; char *cfgfile = SYS_ASOUNDRC; char *initfile = DATADIR "/init/00main"; char *pidfile = SYS_PIDFILE; char *cardname, ncardname[16]; char *cmd; const char *const *tmp; int removestate = 0; int init_fallback = 1; /* new default behavior */ int period = 5*60; int background = 0; int daemoncmd = 0; int use_nice = NO_NICE; int sched_idle = 0; struct arg *a; struct option *o; int i, j, k, res; struct option *long_option; char *short_option; long_option = calloc(ARRAY_SIZE(args), sizeof(struct option)); if (long_option == NULL) exit(EXIT_FAILURE); short_option = malloc(128); if (short_option == NULL) { free(long_option); exit(EXIT_FAILURE); } for (i = j = k = 0; i < ARRAY_SIZE(args); i++) { a = &args[i]; if ((a->sarg & 0xff) == 0) continue; o = &long_option[j]; o->name = a->larg; o->has_arg = (a->sarg & (ENVARG|FILEARG|INTARG)) != 0; o->flag = NULL; o->val = a->sarg & 0xff; j++; short_option[k++] = o->val; if (o->has_arg) short_option[k++] = ':'; } short_option[k] = '\0'; command = argv[0]; while (1) { int c; if ((c = getopt_long(argc, argv, short_option, long_option, NULL)) < 0) break; switch (c) { case 'h': help(); res = EXIT_SUCCESS; goto out; case 'f': cfgfile = optarg; break; case 'l': do_lock = 1; break; case 'F': force_restore = 1; break; case 'g': ignore_nocards = 1; break; case 'E': if (putenv(optarg)) { fprintf(stderr, "environment string '%s' is wrong\n", optarg); res = EXIT_FAILURE; goto out; } break; case 'i': initfile = optarg; break; case 'I': init_fallback = 0; break; case 'r': statefile = optarg; break; case 'R': removestate = 1; break; case 'P': force_restore = 0; break; case 'p': period = atoi(optarg); if (period < 10) period = 5*60; else if (period > 24*60*60) period = 24*60*60; break; case 'e': pidfile = optarg; break; case 'b': background = 1; break; case 's': use_syslog = 1; break; case 'n': use_nice = atoi(optarg); if (use_nice < -20) use_nice = -20; else if (use_nice > 19) use_nice = 19; break; case 'c': sched_idle = 1; break; case 'd': debugflag = 1; break; case 'v': printf("alsactl version " SND_UTIL_VERSION_STR "\n"); res = EXIT_SUCCESS; goto out; case '?': // error msg already printed help(); res = EXIT_FAILURE; goto out; default: // should never happen fprintf(stderr, "Invalid option '%c' (%d) not handled??\n", c, c); } } free(short_option); short_option = NULL; free(long_option); long_option = NULL; if (argc - optind <= 0) { fprintf(stderr, "alsactl: Specify command...\n"); res = 0; goto out; } cardname = argc - optind > 1 ? argv[optind + 1] : NULL; for (tmp = devfiles; cardname != NULL && *tmp != NULL; tmp++) { int len = strlen(*tmp); if (!strncmp(cardname, *tmp, len)) { long l = strtol(cardname + len, NULL, 0); sprintf(ncardname, "%li", l); cardname = ncardname; break; } } /* the global system file should be always locked */ if (strcmp(cfgfile, SYS_ASOUNDRC) == 0) do_lock = 1; /* when running in background, use syslog for reports */ if (background) { use_syslog = 1; daemon(0, 0); } cmd = argv[optind]; daemoncmd = strcmp(cmd, "daemon") == 0 || strcmp(cmd, "rdaemon") == 0; if (use_syslog) { openlog("alsactl", LOG_CONS|LOG_PID, LOG_DAEMON); if (daemoncmd) syslog(LOG_INFO, "alsactl " SND_UTIL_VERSION_STR " daemon started"); } if (!strcmp(cmd, "init")) { res = init(initfile, cardname); snd_config_update_free_global(); } else if (!strcmp(cmd, "store")) { res = save_state(cfgfile, cardname); } else if (!strcmp(cmd, "restore") || !strcmp(cmd, "rdaemon") || !strcmp(cmd, "nrestore")) { if (removestate) remove(statefile); res = load_state(cfgfile, initfile, cardname, init_fallback); if (!strcmp(cmd, "rdaemon")) { do_nice(use_nice, sched_idle); res = state_daemon(cfgfile, cardname, period, pidfile); } if (!strcmp(cmd, "nrestore")) res = state_daemon_kill(pidfile, "rescan"); } else if (!strcmp(cmd, "daemon")) { do_nice(use_nice, sched_idle); res = state_daemon(cfgfile, cardname, period, pidfile); } else if (!strcmp(cmd, "kill")) { res = state_daemon_kill(pidfile, cardname); } else { fprintf(stderr, "alsactl: Unknown command '%s'...\n", cmd); res = -ENODEV; } snd_config_update_free_global(); if (use_syslog) { if (daemoncmd) syslog(LOG_INFO, "alsactl daemon stopped"); closelog(); } return res < 0 ? -res : 0; out: free(short_option); free(long_option); return res; }
/*===========================================================================* * main * *===========================================================================*/ PUBLIC int main(void) { /* Main routine of the scheduler. */ message m_in; /* the incoming message itself is kept here. */ int call_nr; /* system call number */ int who_e; /* caller's endpoint */ int result; /* result to system call */ int rv; /* SEF local startup. */ sef_local_startup(); /* Initialize scheduling timers, used for running balance_queues */ init_scheduling(); /* my Hello code*/ printf("\n%s\n", " ========================================================="); printf("%s\n", " Hello, Minix is now using a lottery scheduler."); printf("%s\n\n", " ========================================================="); /* This is SCHED's main loop - get work and do it, forever and forever. */ while (TRUE) { int ipc_status; /* Wait for the next message and extract useful information from it. */ if (sef_receive_status(ANY, &m_in, &ipc_status) != OK) panic("SCHED sef_receive error"); who_e = m_in.m_source; /* who sent the message */ call_nr = m_in.m_type; /* system call number */ /* Check for system notifications first. Special cases. */ if (is_ipc_notify(ipc_status)) { switch(who_e) { case CLOCK: expire_timers(m_in.NOTIFY_TIMESTAMP); continue; /* don't reply */ default : result = ENOSYS; } goto sendreply; } switch(call_nr) { case SCHEDULING_INHERIT: case SCHEDULING_START: result = do_start_scheduling(&m_in); break; case SCHEDULING_STOP: result = do_stop_scheduling(&m_in); break; case SCHEDULING_SET_NICE: result = do_nice(&m_in); break; case SCHEDULING_NO_QUANTUM: /* This message was sent from the kernel, don't reply */ if (IPC_STATUS_FLAGS_TEST(ipc_status, IPC_FLG_MSG_FROM_KERNEL)) { if ((rv = do_noquantum(&m_in)) != (OK)) { printf("SCHED: Warning, do_noquantum " "failed with %d\n", rv); } continue; /* Don't reply */ } else { printf("SCHED: process %d faked " "SCHEDULING_NO_QUANTUM message!\n", who_e); result = EPERM; } break; default: result = no_sys(who_e, call_nr); } sendreply: /* Send reply. */ if (result != SUSPEND) { m_in.m_type = result; /* build reply message */ reply(who_e, &m_in); /* send it away */ } } return(OK); }
int main() { int count; char *rest, *command_str, *param_str, *param2_str; //Inicializar terminal... devreq(KEYBOARD); devreq(SCREEN); devreq(TERMINAL); while(TRUE) { write(TERMINAL, PROMPT, strlen(PROMPT)); count = read_line(line_buffer, BUFF_LEN); if (count == -1) { while (read_line(line_buffer, BUFF_LEN) != 1); write_line("Line too long"); } else { line_buffer[count -1] = NULL; rest = skip_spaces(line_buffer); if (!*rest) { command_use_error(); continue; } command_str = get_word(&rest); rest = skip_spaces(rest); param_str = get_word(&rest); rest = skip_spaces(rest); if (strcmp(HELP, command_str) == 0) { print_shell_use(); } else if (strcmp(ECHO, command_str) == 0) { write_line(param_str); } else if (strcmp(PS, command_str) == 0) { //Imprimir informacion de programas en ejecucion print_ps(param_str); } else if (strcmp(LS, command_str) == 0) { //Imprimir lista de programas disponibles print_ls(param_str); } else if (strcmp(RUN, command_str) == 0) { //Ejecutar un programa en foreground do_run(param_str); } else if (strcmp(RUN_BG, command_str) == 0) { //Ejecutar un programa en background do_runbg(param_str); } else if (strcmp(NICE, command_str) == 0) { param2_str = get_word(&rest); //Cambiar prioridad de un proceso do_nice(param_str, param2_str); } else if (strcmp(KILL, command_str) == 0) { //Ejecutar un programa en background do_kill(param_str); } else command_use_error(); } } return 0; }