static int resizetty(void) { /* * \e7 Save current state (cursor coordinates, attributes, * character sets pointed at by G0, G1). * \e[r Set scrolling region; parameters are top and bottom row. * \e[32766E Move cursor down 32766 (INT16_MAX - 1) rows. * \e[32766C Move cursor right 32766 columns. * \e[6n Report cursor position. * \e8 Restore state most recently saved by \e7. */ static const char *getpos = "\e7\e[r\e[32766E\e[32766C\e[6n\e8"; char retstr[32]; int row, col; size_t pos; ssize_t rc; struct winsize ws; struct termios saved_attributes; int saved_fl; if (!isatty(STDIN_FILENO)) errx(EXIT_FAILURE, _("stdin does not refer to a terminal")); tty_raw(&saved_attributes, &saved_fl); if (write_all(STDIN_FILENO, getpos, strlen(getpos)) < 0) { warn(_("write failed")); tty_restore(&saved_attributes, &saved_fl); return 1; } for (pos = 0; pos < sizeof(retstr) - 1;) { if (0 == select_wait()) break; if ((rc = read(STDIN_FILENO, retstr + pos, sizeof(retstr) - 1 - pos)) < 0) { if (errno == EINTR) continue; warn(_("read failed")); tty_restore(&saved_attributes, &saved_fl); return 1; } pos += rc; if (retstr[pos - 1] == 'R') break; } retstr[pos] = 0; tty_restore(&saved_attributes, &saved_fl); rc = sscanf(retstr, "\033[%d;%dR", &row, &col); if (rc != 2) { warnx(_("invalid cursor position: %s"), retstr); return 1; } memset(&ws, 0, sizeof(struct winsize)); ioctl(STDIN_FILENO, TIOCGWINSZ, &ws); ws.ws_row = row; ws.ws_col = col; ioctl(STDIN_FILENO, TIOCSWINSZ, &ws); return 0; }
/* Called when window is resized. */ static void sigwinch() { tty_restore(); tty_raw(); action(NULL); }
/* Called when window is resized. */ static void sigwinch(int dummy) { UNUSED(dummy); tty_restore(); tty_raw(); action(NULL); }
/* Close down a terminal line. */ static int tty_close(void) { /* * Set the old discipline and restore the * previous line mode. */ (void) tty_set_ldisc(ldisc_before); (void) tty_restore(); (void) tty_lock(NULL, 0); return 0; }
static int tty_esc_type_esc(tty_t t, char c, int pos) { switch(c) { case '[': /* CSI START */ t->escape_type = ESC_TYPE_CSI; cprintf("Started CSI sequence"); return ESC_RES_CNT; case '(': /* Define G0 character set */ case ')': /* Define G1 character set */ case ']': /* OS command (change colors) */ return ESC_RES_CNT; case 'D': /* Line feed */ case 'E': /* New line */ tty_new_line(t); return ESC_RES_DNE; case 'H': /* Set tab stop at curr pos */ cprintf("Old tab stop: %d", t->tab_stop); t->tab_stop = t->cursor_pos % CONSOLE_COLS; cprintf("New tab stop: %d", t->tab_stop); return ESC_RES_DNE; case '7': /* Save tty state */ tty_save(t); return ESC_RES_DNE; case '8': /* restore tty state */ tty_restore(t); return ESC_RES_DNE; case 'Z': /* Identify self */ tty_handle_raw(0x1B, t); tty_handle_raw('/', t); tty_handle_raw('Z', t); return ESC_RES_DNE; /* Unimplemented escape sequences */ case '#': /* Possible alignment test */ case 'c': /* Reset the terminal properties */ case 'M': /* Reverse line feed */ case '>': /* Set numeric keyboard mode */ case '=': /* Set application keyboard mode */ default: /* Unhandled */ return ESC_RES_ERR; } }
static void finish(const char *psz_msg, int rc) { if (b_interactive) { attron(A_STANDOUT); mvprintw(LINE_ACTION, 0, (char *) "%s, exiting...\n", psz_msg); attroff(A_STANDOUT); clrtoeol(); refresh(); } tty_restore(); #ifdef HAVE_CDDB if (p_conn) cddb_destroy(p_conn); cddb_disc_destroy(p_cddb_disc); libcddb_shutdown(); #endif /*HAVE_CDDB*/ cdio_destroy (p_cdio); free (psz_device); exit (rc); }
int tty_init(struct lt_config_app *cfg, int master) { int i, slave, num_files = getdtablesize(); char *sname; jmp_buf env; if (setjmp(env)) { tty_restore(cfg); return -1; } sname = (char*) ptsname(master); if (!sname) longjmp(env, 1); PRINT_VERBOSE(cfg, 1, "closing all opened descriptors\n"); for(i = 0; i < num_files; i++) close(i); /* get new session before we open new controling tty */ if (-1 == setsid()) { perror("setsid failed"); return -1; } slave = open(sname, O_RDWR); if (slave != 0) longjmp(env, 1); /* set controling tty */ if (ioctl(0, TIOCSCTTY, 1)) longjmp(env, 1); if (setup_slave(0)) longjmp(env, 1); dup(0); dup(0); return 0; }
static void sigpipe(int s) { tty_restore(); fprintf(stderr, "Broken pipe. Disk full?\n"); exit(0); }
/*------------------------------------------------------------------------------ main() Main Preconditions: Postconditions: *----------------------------------------------------------------------------*/ int main( int argc, char **argv ) /*----------------------------------------------------------------------------*/ { int user_input; char user_str[128]; ws_init(); tty_init(); // rmcpd_init_listener(); printf("\nIPMI Exerciser -- %s\n", __DATE__); // say Hi process_command_line( argc, argv ); // process command line arguments // Get user keyboard input while( 1 ) { main_menu(); scanf( "%d", &user_input ); fflush( stdin ); switch ( user_input ) { case KBD_APP_CMDS: kbd_app_cmds(); break; case KBD_CHASSIS_CMDS: break; case KBD_EVENT_CMDS: break; case KBD_FIRMWARE_COMMANDS: break; case KBD_NVSTORE_CMDS: break; case KBD_MEDIA_SPECIFIC_CMDS: break; case KBD_PICMG_CMDS: kbd_atca_cmds(); break; case KBD_RUN_UNIX: printf( "Enter command : " ); scanf( "%s", user_str ); //gets( user_str ); fflush( stdin ); if( system( user_str ) == -1 ) printf( "\nError in command\n" ); printf( "\n Press any key to continue\n" ); getchar(); fflush( stdin ); break; case KBD_SETTINGS: kbd_settings(); break; case KBD_QUIT: tty_restore(); exit( EXIT_SUCCESS ); break; default: printf( "Unknown option %d ignored.\n", user_input ); break; } // end switch printf( "Press any key to continue\n" ); getchar(); fflush( stdin ); } }
int main(int argc, char *argv[]) { int c, nostop=0; char *h; int i_rc = 0; cd_operation_t cd_op = NO_OP; /* operation to do in non-interactive mode */ psz_program = strrchr(argv[0],'/'); psz_program = psz_program ? psz_program+1 : argv[0]; memset(&cddb_opts, 0, sizeof(cddb_opts)); cdio_loglevel_default = CDIO_LOG_WARN; /* parse options */ while ( 1 ) { if (-1 == (c = getopt(argc, argv, "acCdehkplL:sSt:vx"))) break; switch (c) { case 'v': b_verbose = true; if (cdio_loglevel_default > CDIO_LOG_INFO) cdio_loglevel_default = CDIO_LOG_INFO; break; case 'd': debug = 1; if (cdio_loglevel_default > CDIO_LOG_DEBUG) cdio_loglevel_default = CDIO_LOG_DEBUG; break; case 'a': auto_mode = 1; break; case 'L': i_volume_level = atoi(optarg); cd_op = SET_VOLUME; b_interactive = false; break; case 't': if (NULL != (h = strchr(optarg,'-'))) { *h = 0; start_track = atoi(optarg); stop_track = atoi(h+1)+1; if (0 == start_track) start_track = 1; if (1 == stop_track) stop_track = CDIO_CDROM_LEADOUT_TRACK; } else { start_track = atoi(optarg); stop_track = start_track+1; one_track = 1; } b_interactive = false; cd_op = PLAY_TRACK; break; case 'p': b_interactive = false; cd_op = PLAY_CD; break; case 'l': b_interactive = false; cd_op = LIST_TRACKS; break; case 'C': b_interactive = false; cd_op = CLOSE_CD; break; case 'c': b_interactive = false; cd_op = PS_LIST_TRACKS; break; case 's': b_interactive = false; cd_op = STOP_PLAYING; break; case 'S': b_interactive = false; cd_op = LIST_SUBCHANNEL; break; case 'e': b_interactive = false; cd_op = EJECT_CD; break; case 'k': print_keys(); exit(1); case 'h': usage(psz_program); exit(1); default: usage(psz_program); exit(1); } } if (argc > optind) { psz_device = strdup(argv[optind]); } else { char **ppsz_cdda_drives=NULL; char **ppsz_all_cd_drives = cdio_get_devices_ret(&driver_id); if (!ppsz_all_cd_drives) { fprintf(stderr, "Can't find a CD-ROM drive\n"); exit(2); } ppsz_cdda_drives = cdio_get_devices_with_cap(ppsz_all_cd_drives, CDIO_FS_AUDIO, false); if (!ppsz_cdda_drives || !ppsz_cdda_drives[0]) { fprintf(stderr, "Can't find a CD-ROM drive with a CD-DA in it\n"); exit(3); } psz_device = strdup(ppsz_cdda_drives[0]); cdio_free_device_list(ppsz_all_cd_drives); cdio_free_device_list(ppsz_cdda_drives); } if (!b_interactive) { b_sig = true; nostop=1; } tty_raw(); signal(SIGINT,ctrlc); signal(SIGQUIT,ctrlc); signal(SIGTERM,ctrlc); signal(SIGHUP,ctrlc); signal(SIGWINCH, sigwinch); if (CLOSE_CD != cd_op) { /* open device */ if (b_verbose) fprintf(stderr, "open %s... ", psz_device); p_cdio = cdio_open (psz_device, driver_id); if (!p_cdio && cd_op != EJECT_CD) { cd_close(psz_device); p_cdio = cdio_open (psz_device, driver_id); } if (p_cdio && b_verbose) fprintf(stderr,"ok\n"); } if (b_interactive) { #ifdef HAVE_CDDB cddb_log_set_handler (cddb_log_handler); #else ; #endif } else { b_sig = true; nostop=1; if (EJECT_CD == cd_op) { i_rc = cd_eject() ? 0 : 1; } else { switch (cd_op) { case PS_LIST_TRACKS: case LIST_TRACKS: case PLAY_TRACK: read_toc(p_cdio); default: break; } if (p_cdio) switch (cd_op) { case STOP_PLAYING: b_cd = true; i_rc = cd_stop(p_cdio) ? 0 : 1; break; case EJECT_CD: /* Should have been handled above. */ cd_eject(); break; case LIST_TRACKS: list_tracks(); break; case PS_LIST_TRACKS: ps_list_tracks(); break; case PLAY_TRACK: /* play just this one track */ if (b_record) { printf("%s / %s\n", artist, title); if (one_track) printf("%s\n", cd_info[start_track].title); } i_rc = play_track(start_track, stop_track) ? 0 : 1; break; case PLAY_CD: if (b_record) printf("%s / %s\n", artist, title); play_track(1,CDIO_CDROM_LEADOUT_TRACK); break; case SET_VOLUME: i_rc = set_volume_level(p_cdio, i_volume_level); break; case LIST_SUBCHANNEL: if (read_subchannel(p_cdio)) { if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PAUSED || sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) { { printf("track %2d - %02x:%02x (%02x:%02x abs) ", sub.track, sub.rel_addr.m, sub.rel_addr.s, sub.abs_addr.m, sub.abs_addr.s); } } printf("drive state: %s\n", mmc_audio_state2str(sub.audio_status)); } else { i_rc = 1; } break; case CLOSE_CD: /* Handled below */ case LIST_KEYS: case TOGGLE_PAUSE: case EXIT_PROGRAM: case NO_OP: break; } else if (CLOSE_CD == cd_op) { i_rc = (DRIVER_OP_SUCCESS == cdio_close_tray(psz_device, NULL)) ? 0 : 1; } else { fprintf(stderr,"no CD in drive (%s)\n", psz_device); } } } /* Play all tracks *unless* we have a play or paused status already. */ read_subchannel(p_cdio); if (sub.audio_status != CDIO_MMC_READ_SUB_ST_PAUSED && sub.audio_status != CDIO_MMC_READ_SUB_ST_PLAY) play_track(1, CDIO_CDROM_LEADOUT_TRACK); while ( !b_sig ) { int key; if (!b_cd) read_toc(p_cdio); read_subchannel(p_cdio); display_status(false); if (1 == select_wait(b_cd ? 1 : 5)) { switch (key = getch()) { case '-': decrease_volume_level(p_cdio); break; case '+': increase_volume_level(p_cdio); break; case 'A': case 'a': auto_mode = !auto_mode; break; case 'X': case 'x': nostop=1; /* fall through */ case 'Q': case 'q': b_sig = true; break; case 'E': case 'e': cd_eject(); break; case 's': cd_stop(p_cdio); break; case 'C': case 'c': cd_close(psz_device); break; case 'L': case 'l': b_all_tracks = !b_all_tracks; if (b_all_tracks) display_tracks(); else { i_last_display_track = CDIO_INVALID_TRACK; display_cdinfo(p_cdio, i_tracks, i_first_track); } break; case 'K': case 'k': case 'h': case 'H': case '?': list_keys(); break; case ' ': case 'P': case 'p': toggle_pause(); break; case KEY_RIGHT: if (b_cd && (sub.audio_status == CDIO_MMC_READ_SUB_ST_PAUSED || sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY)) play_track(sub.track+1, CDIO_CDROM_LEADOUT_TRACK); else play_track(1,CDIO_CDROM_LEADOUT_TRACK); break; case KEY_LEFT: if (b_cd && (sub.audio_status == CDIO_MMC_READ_SUB_ST_PAUSED || sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY)) play_track(sub.track-1,CDIO_CDROM_LEADOUT_TRACK); break; case KEY_UP: if (b_cd && sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) skip(10); break; case KEY_DOWN: if (b_cd && sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) skip(-10); break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': play_track(key - '0', CDIO_CDROM_LEADOUT_TRACK); break; case '0': play_track(10, CDIO_CDROM_LEADOUT_TRACK); break; case KEY_F(1): case KEY_F(2): case KEY_F(3): case KEY_F(4): case KEY_F(5): case KEY_F(6): case KEY_F(7): case KEY_F(8): case KEY_F(9): case KEY_F(10): case KEY_F(11): case KEY_F(12): case KEY_F(13): case KEY_F(14): case KEY_F(15): case KEY_F(16): case KEY_F(17): case KEY_F(18): case KEY_F(19): case KEY_F(20): play_track(key - KEY_F(1) + 11, CDIO_CDROM_LEADOUT_TRACK); break; } } } if (!nostop) cd_stop(p_cdio); tty_restore(); finish("bye", i_rc); return 0; /* keep compiler happy */ }
int main(int argc, char *argv[]) { int ret; const char *cid; char cmd[CMD_SIZE]; GError *err = NULL; _cleanup_free_ char *contents; int cpid = -1; int status; pid_t pid; _cleanup_close_ int mfd = -1; _cleanup_close_ int epfd = -1; char slname[BUF_SIZE]; char buf[BUF_SIZE]; int num_read; struct termios t; struct epoll_event ev; struct epoll_event evlist[MAX_EVENTS]; if (argc < 2) { nexit("Run as: conmon <id>"); } /* Get the container id */ cid = argv[1]; /* * Set self as subreaper so we can wait for container process * and return its exit code. */ ret = prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0); if (ret != 0) { pexit("Failed to set as subreaper"); } /* Open the master pty */ mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY); if (mfd < 0) pexit("Failed to open console master pty"); /* Grant access to the slave pty */ if (grantpt(mfd) == -1) pexit("Failed to grant access to slave pty"); /* Unlock the slave pty */ if (unlockpt(mfd) == -1) { /* Unlock slave pty */ pexit("Failed to unlock the slave pty"); } /* Get the slave pty name */ ret = ptsname_r(mfd, slname, BUF_SIZE); if (ret != 0) { pexit("Failed to get the slave pty name"); } /* Create the container */ snprintf(cmd, CMD_SIZE, "runc create %s --pid-file pidfile --console %s", cid, slname); ret = system(cmd); if (ret != 0) { nexit("Failed to create container"); } /* Read the pid so we can wait for the process to exit */ g_file_get_contents("pidfile", &contents, NULL, &err); if (err) { fprintf(stderr, "Failed to read pidfile: %s\n", err->message); g_error_free(err); exit(1); } cpid = atoi(contents); printf("container PID: %d\n", cpid); /* Save exiting termios settings */ if (tcgetattr(STDIN_FILENO, &tty_orig) == -1) pexit("tcegetattr"); /* Settings for raw mode */ t.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL | IEXTEN); t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP | IXON | IXOFF | IGNPAR | PARMRK); t.c_oflag &= ~OPOST; t.c_cc[VMIN] = 1; t.c_cc[VTIME] = 0; /* Set terminal to raw mode */ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t) == -1) pexit("tcsetattr"); /* Setup terminal restore on exit */ if (atexit(tty_restore) != 0) pexit("atexit"); epfd = epoll_create(5); if (epfd < 0) pexit("epoll_create"); ev.events = EPOLLIN; ev.data.fd = STDIN_FILENO; if (epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) < 0) { pexit("Failed to add stdin to epoll"); } ev.data.fd = mfd; if (epoll_ctl(epfd, EPOLL_CTL_ADD, mfd, &ev) < 0) { pexit("Failed to add console master fd to epoll"); } /* Copy data back and forth between STDIN and master fd */ while (true) { int ready = epoll_wait(epfd, evlist, MAX_EVENTS, -1); for (int i = 0; i < ready; i++) { if (evlist[i].events & EPOLLIN) { if (evlist[i].data.fd == STDIN_FILENO) { num_read = read(STDIN_FILENO, buf, BUF_SIZE); if (num_read <= 0) goto out; if (write(mfd, buf, num_read) != num_read) { nwarn("partial/failed write (masterFd)"); goto out; } } else if (evlist[i].data.fd == mfd) { num_read = read(mfd, buf, BUF_SIZE); if (num_read <= 0) goto out; if (write(STDOUT_FILENO, buf, num_read) != num_read) { nwarn("partial/failed write (STDOUT_FILENO)"); goto out; } } } else if (evlist[i].events & (EPOLLHUP | EPOLLERR)) { printf("closing fd %d\n", evlist[i].data.fd); if (close(evlist[i].data.fd) < 0) pexit("close"); goto out; } } } out: tty_restore(); /* Wait for the container process and record its exit code */ while ((pid = waitpid(-1, &status, 0)) > 0) { printf("PID %d exited\n", pid); if (pid == cpid) { _cleanup_free_ char *status_str = NULL; ret = asprintf(&status_str, "%d", status); if (ret < 0) { pexit("Failed to allocate memory for status"); } g_file_set_contents("exit", status_str, strlen(status_str), &err); if (err) { fprintf(stderr, "Failed to write %s to exit file: %s\n", status_str, err->message); g_error_free(err); exit(1); } break; } } return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { char *p; char *host = 0, *term = 0; int port = LIRCD_UDP_PORT; int foreground = 0; int wait_term = 0; int tcp = 0; int speed = B38400; int tty; int c; prog = argv[0]; p = strrchr(argv[0], '/'); if (p) prog = p + 1; while ((c = getopt(argc, argv, "HdDTfw:t:h:p:")) != EOF) { switch (c) { case 'H': speed = B115200; break; case 'd': debug = DEBUG_AND_CONNECT; break; case 'D': debug = DEBUG_ONLY; break; case 'f': foreground = 1; break; case 't': term = optarg; break; case 'w': wait_term = atoi(optarg); if (wait_term == 0) usage(); break; case 'h': host = optarg; break; case 'T': tcp = 1; break; case 'p': /* or microseconds */ port = atoi(optarg); break; default: usage(); break; } } if ((debug != DEBUG_ONLY && !host) || !term || optind != argc) { usage(); } while (1) { tty = tty_init(term, wait_term, speed); if (!daemonized && !foreground && !debug) { if (daemon(0, 0) < 0) die("daemon"); daemonized = 1; } data_loop(tty, tcp, host, port); /* we'll only ever return from data_loop() if our read() * returns 0, which usually means our (USB-based) tty has * gone away. loop if we were told to wait (-w) for it. */ if (!wait_term) die("end-of-dataloop"); tty_restore(); close(tty); } return 0; }
void sighandler(int sig) { tty_restore(); die("signal"); }