/* * Run kermit. Used to do this in the main window, but newer * versions of kermit are too intelligent and just want a tty * for themselves or they won't function ok. Shame. */ void kermit(void) { int status, pid, n; char * translated_cmdline; char *kermit_path = P_KERMIT; if (!kermit_path || !*kermit_path) { werror("No kermit path defined!"); return; } /* Clear screen, set keyboard modes etc. */ mc_wleave(); switch (pid = fork()) { case -1: mc_wreturn(); werror(_("Out of memory: could not fork()")); return; case 0: /* Child */ close(portfd); /* Remove lockfile */ lockfile_remove(); for (n = 0; n < _NSIG; n++) signal(n, SIG_DFL); translated_cmdline = translate(P_KERMIT); if (translated_cmdline != NULL) { fastexec(translated_cmdline); free(translated_cmdline); } exit(1); default: /* Parent */ break; } m_wait(&status); /* Restore screen and keyboard modes */ mc_wreturn(); /* Re-create lockfile */ lockfile_create(); m_flush(portfd); port_init(); }
int get_lock() { struct stat stt; char buf[128]; int fd, n = 0; have_lock_dir(); if((fd = open(lockfile, O_RDONLY)) >= 0) { n = read(fd, buf, 127); close(fd); if (n > 0) { pid = -1; if (n == 4) /* Kermit-style lockfile. */ pid = *(int *)buf; else { /* Ascii lockfile. */ buf[n] = 0; sscanf(buf, "%d", &pid); } if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) { printf("Lockfile is stale. Overriding it..\n"); sleep(1); unlink(lockfile); } else n = 0; } if (n == 0) { if(retry == 1) /* Last retry */ printf("Device %s is locked.\n", dial_tty); return 0; } } lockfile_create(); return 1; }
/* * Open the terminal. * * \return -1 on error, 0 on success */ int open_term(int doinit, int show_win_on_error, int no_msgs) { struct stat stt; union { char bytes[128]; int kermit; } buf; int fd, n = 0; int pid; #ifdef HAVE_ERRNO_H int s_errno; #endif #ifdef USE_SOCKET #define SOCKET_PREFIX "unix#" portfd_is_socket = portfd_is_connected = 0; if (strncmp(dial_tty, SOCKET_PREFIX, strlen(SOCKET_PREFIX)) == 0) { portfd_is_socket = 1; } #endif if (portfd_is_socket) goto nolock; #if !HAVE_LOCKDEV /* First see if the lock file directory is present. */ if (P_LOCK[0] && stat(P_LOCK, &stt) == 0) { #ifdef SVR4_LOCKS stat(dial_tty, &stt); sprintf(lockfile, "%s/LK.%03d.%03d.%03d", P_LOCK, major(stt.st_dev), major(stt.st_rdev), minor(stt.st_rdev)); #else /* SVR4_LOCKS */ snprintf(lockfile, sizeof(lockfile), "%s/LCK..%s", P_LOCK, mdevlockname(dial_tty, buf.bytes, sizeof(buf.bytes))); #endif /* SVR4_LOCKS */ } else lockfile[0] = 0; if (doinit > 0 && lockfile[0] && (fd = open(lockfile, O_RDONLY)) >= 0) { n = read(fd, buf.bytes, 127); close(fd); if (n > 0) { pid = -1; if (n == 4) /* Kermit-style lockfile. */ pid = buf.kermit; else { /* Ascii lockfile. */ buf.bytes[n] = 0; sscanf(buf.bytes, "%d", &pid); } if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) { fprintf(stderr, _("Lockfile is stale. Overriding it..\n")); sleep(1); unlink(lockfile); } else n = 0; } if (n == 0) { if (stdwin) mc_wclose(stdwin, 1); fprintf(stderr, _("Device %s is locked.\n"), dial_tty); return -1; } } #endif if (doinit > 0 && lockfile_create(no_msgs) != 0) return -1; nolock: /* Run a special program to disable callin if needed. */ if (doinit > 0 && P_CALLOUT[0]) { if (fastsystem(P_CALLOUT, NULL, NULL, NULL) < 0) { if (stdwin) mc_wclose(stdwin, 1); fprintf(stderr, _("Could not setup for dial out.\n")); lockfile_remove(); return -1; } } /* Now open the tty device. */ if (setjmp(albuf) == 0) { portfd = -1; signal(SIGALRM, get_alrm); alarm(20); #ifdef USE_SOCKET if (portfd_is_socket) { portfd_sock_addr.sun_family = AF_UNIX; strncpy(portfd_sock_addr.sun_path, dial_tty + strlen(SOCKET_PREFIX), sizeof(portfd_sock_addr.sun_path)-1); portfd_sock_addr.sun_path[sizeof(portfd_sock_addr.sun_path)-1] = 0; term_socket_connect(); } #endif /* USE_SOCKET */ if (!portfd_is_socket) { #if defined(O_NDELAY) && defined(F_SETFL) portfd = open(dial_tty, O_RDWR|O_NDELAY|O_NOCTTY); if (portfd >= 0) { /* Cancel the O_NDELAY flag. */ n = fcntl(portfd, F_GETFL, 0); fcntl(portfd, F_SETFL, n & ~O_NDELAY); } #else if (portfd < 0) portfd = open(dial_tty, O_RDWR|O_NOCTTY); #endif } if (portfd >= 0) { if (doinit > 0) m_savestate(portfd); port_init(); } } #ifdef HAVE_ERRNO_H s_errno = errno; #endif alarm(0); signal(SIGALRM, SIG_IGN); if (portfd < 0 && !portfd_is_socket) { if (!no_msgs) { if (doinit > 0) { if (stdwin) mc_wclose(stdwin, 1); #ifdef HAVE_ERRNO_H fprintf(stderr, _("minicom: cannot open %s: %s\n"), dial_tty, strerror(s_errno)); #else fprintf(stderr, _("minicom: cannot open %s. Sorry.\n"), dial_tty); #endif lockfile_remove(); return -1; } if (show_win_on_error) werror(_("Cannot open %s!"), dial_tty); } lockfile_remove(); return -1; } /* Set CLOCAL mode */ m_nohang(portfd); /* Set Hangup on Close if program crashes. (Hehe) */ m_hupcl(portfd, 1); if (doinit > 0) m_flush(portfd); return 0; }
void updown(int what, int nr) { #ifdef LOG_XFER #warning LOG_XFER defined! FILE *xfl; #endif const char *name[13]; int idx[13]; int r, f, g = 0; char *t = what == 'U' ? _("Upload") : _("Download"); char buf[160]; char buffirst[20]; char xfrstr[160] = ""; char trimbuf[160] = ""; char title[64]; const char *s =""; int pipefd[2]; int n, status; char * cmdline = NULL; char * translated_cmdline = NULL; WIN *win = (WIN *)NULL; if (mcd(what == 'U' ? P_UPDIR : P_DOWNDIR) < 0) return; /* Automatic? */ if (nr == 0) { for (f = 0; f < 12; f++) { if (P_PNAME(f)[0] && P_PUD(f) == what) { name[g] = P_PNAME(f); idx[g++] = f; } } name[g] = NULL; if (g == 0) return; r = mc_wselect(30, 7, name, NULL, t, stdattr, mfcolor, mbcolor) - 1; if (r < 0) return; g = idx[r]; } else g = nr; buf[0] = 0; /* jseymour file selector with choice of dir on zmodem, etc. download */ #if 1 { int multiple; /* 0:only directory, 1:one file, -1:any number */ size_t cmdline_length; if (P_MUL(g)=='Y') /* need file(s), or just a directory? */ multiple = what == 'U'? -1 : 0; else multiple = 1; /* only one allowed */ if (P_FSELW[0] == 'Y' && (what == 'U' || P_ASKDNDIR[0] == 'Y')) { s = filedir(multiple, what == 'U'? 0 : 1); if (s == NULL) return; } else if (P_PNN(g) == 'Y') { s = input(_("Please enter file names"), buf); if (s == NULL) return; } /* discard directory if "multiple" == 0 */ cmdline_length = strlen(P_PPROG(g)) + strlen((char*) (multiple == 0 ? "" : s)) + 1; /* + 1 for ' ' */ cmdline = malloc(cmdline_length + 1); /* + 1 for NUL */ if (cmdline == NULL) { werror(_("Out of memory: could allocate buffer for command line")); return; } snprintf(cmdline, cmdline_length + 1, "%s %s", P_PPROG(g), multiple == 0 ? "" : s); } #endif if (P_LOGXFER[0] == 'Y') do_log("%s", cmdline); /* jl 22.06.97 */ if (P_PFULL(g) == 'N') { win = mc_wopen(10, 7, 70, 13, BSINGLE, stdattr, mfcolor, mbcolor, 1, 0, 1); snprintf(title, sizeof(title), _("%.30s %s - Press CTRL-C to quit"), P_PNAME(g), what == 'U' ? _("upload") : _("download")); mc_wtitle(win, TMID, title); pipe(pipefd); } else mc_wleave(); m_flush(portfd); switch (udpid = fork()) { case -1: werror(_("Out of memory: could not fork()")); if (win) { close(pipefd[0]); close(pipefd[1]); mc_wclose(win, 1); } else mc_wreturn(); mcd(""); if(cmdline) free(cmdline); return; case 0: /* Child */ if (P_PIORED(g) == 'Y') { dup2(portfd, 0); dup2(portfd, 1); } if (win) { dup2(pipefd[1], 2); close(pipefd[0]); if (pipefd[1] != 2) close(pipefd[1]); } lockfile_remove(); for (n = 1; n < _NSIG; n++) signal(n, SIG_DFL); translated_cmdline = translate(cmdline); if (translated_cmdline != NULL) { fastexec(translated_cmdline); free(translated_cmdline); } if(cmdline) free(cmdline); exit(1); default: /* Parent */ break; } if(cmdline) free(cmdline); if (win) { setcbreak(1); /* Cbreak, no echo. */ enab_sig(1, 0); /* But enable SIGINT */ } signal(SIGINT, udcatch); if (P_PIORED(g) == 'Y') { close(pipefd[1]); #ifdef LOG_XFER xfl=fopen("xfer.log","wb"); #endif while ((n = read(pipefd[0], buf, sizeof(buf))) > 0) { buf[n] = '\0'; mc_wputs(win, buf); timer_update(); /* Log the filenames & sizes jl 14.09.97 */ if (P_LOGXFER[0] == 'Y') { #ifdef LOG_XFER if (xfl) fprintf(xfl,">%s<\n",buf); #endif if (sscanf(buf, "%19s", buffirst)) { /* if / jl 29.09.97 */ if (!strncmp (buffirst, "Receiving", 9) || !strncmp (buffirst, "Sending", 7)) { if (xfrstr[0]) { trim (trimbuf, xfrstr, sizeof(trimbuf)); do_log ("%s", trimbuf); xfrstr[0] = 0; } trim (trimbuf, buf, sizeof(trimbuf)); do_log("%s", trimbuf); } else if (!strncmp (buffirst, "Bytes", 5)) { strncpy (xfrstr, buf, sizeof(xfrstr)); } buffirst[0] = 0; trimbuf[0] = 0; } } } #ifdef LOG_XFER if (xfl) fclose(xfl); #endif } /* Log the last file size jl 14.09.97 */ if (P_LOGXFER[0] == 'Y' && xfrstr[0]) { trim (trimbuf, xfrstr, sizeof(trimbuf)); do_log ("%s", trimbuf); xfrstr[0] = 0; } while (udpid != m_wait(&status)); if (win) { enab_sig(0, 0); signal(SIGINT, SIG_IGN); } if (win == (WIN *)0) mc_wreturn(); lockfile_create(); /* MARK updated 02/17/94 - Flush modem port before displaying READY msg */ /* because a BBS often displays menu text right after a download, and we */ /* don't want the modem buffer to be lost while waiting for key to be hit */ m_flush(portfd); port_init(); setcbreak(2); /* Raw, no echo. */ if (win) close(pipefd[0]); mcd(""); timer_update(); /* If we got interrupted, status != 0 */ if (win && (status & 0xFF00) == 0) { #if VC_MUSIC if (P_SOUND[0] == 'Y') { mc_wprintf(win, _("\n READY: press any key to continue...")); music(); } else sleep(1); #else /* MARK updated 02/17/94 - If there was no VC_MUSIC capability, */ /* then at least make some beeps! */ if (P_SOUND[0] == 'Y') mc_wprintf(win, "\007\007\007"); sleep(1); #endif } if (win) mc_wclose(win, 1); }