freeing device file handle upon device unplugging?

Adam Lackorzynski adam at os.inf.tu-dresden.de
Fri Jul 16 08:51:51 UTC 2010


Hi,

On Wed Jul 14, 2010 at 08:56:14 +0200, Alexander Stein wrote:
> I use USB-RS232 converters very regularly. I noticed that if I use minicom on 
> my /dev/ttyUSB0 and the converter is unplugged the device file is removed. So 
> far so good, but minicom doesn't close the file handle resulting upon plugging 
> the convert in again. I get /dev/ttyUSB1 instead. So have to create several 
> configs to cover the most common situations.
> The biggest problem here is with /dev/ttyACM0 which is created by using USB-
> CDC. This device file will be deleted on each reboot of the remote system 
> (embedded boar din this case). So I have to make sure I quit minicom each time 
> otherwise I'll get a different device file than before.
> At least minicom should free the device file handle so upon creation the same 
> name is free at least. Maybe there is also the possibility to reconnect (see 
> ACM problem)

What about this? (against hg repo)


diff -r a041b108875e src/main.c
--- a/src/main.c	Sun Jun 06 11:52:25 2010 +0200
+++ b/src/main.c	Fri Jul 16 10:50:39 2010 +0200
@@ -151,7 +151,7 @@
  *
  * \return -1 on error, 0 on success
  */
-int open_term(int doinit, int show_win_on_error)
+int open_term(int doinit, int show_win_on_error, int no_msgs)
 {
   struct stat stt;
   union {
@@ -270,21 +270,25 @@
   alarm(0);
   signal(SIGALRM, SIG_IGN);
   if (portfd < 0 && !portfd_is_socket) {
-    if (doinit > 0) {
-      if (stdwin)
-	mc_wclose(stdwin, 1);
+    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));
+	fprintf(stderr, _("minicom: cannot open %s: %s\n"),
+			dial_tty, strerror(s_errno));
 #else
-      fprintf(stderr, _("minicom: cannot open %s. Sorry.\n"), dial_tty);
+	fprintf(stderr, _("minicom: cannot open %s. Sorry.\n"), dial_tty);
 #endif
-      lockfile_remove();
-      return -1;
+        lockfile_remove();
+        return -1;
+      }
+
+      if (show_win_on_error)
+	werror(_("Cannot open %s!"), dial_tty);
     }
+
     lockfile_remove();
-    if (show_win_on_error)
-      werror(_("Cannot open %s!"), dial_tty);
     return -1;
   }
 
@@ -709,7 +713,14 @@
 
     /* check if device is ok, if not, try to open it */
     if (!get_device_status(portfd_connected)) {
-      if (open_term(0, 0) < 0) {
+      /* Ok, it's gone, most probably someone unplugged the USB-serial, we
+       * need to free the FD so that a replug can get the same device
+       * filename, open it again and be back */
+      int reopen = portfd == -1;
+      close(portfd);
+      lockfile_remove();
+      portfd = -1;
+      if (open_term(reopen, reopen, 1) < 0) {
         if (!error_on_open_window)
           error_on_open_window = mc_tell(_("Cannot open %s!"), dial_tty);
       } else {
diff -r a041b108875e src/minicom.c
--- a/src/minicom.c	Sun Jun 06 11:52:25 2010 +0200
+++ b/src/minicom.c	Fri Jul 16 10:50:39 2010 +0200
@@ -1281,14 +1281,14 @@
 
   if (dial_tty == NULL) {
     if (!dosetup) {
-      while ((dial_tty = get_port(P_PORT)) != NULL && open_term(doinit, 1) < 0)
+      while ((dial_tty = get_port(P_PORT)) != NULL && open_term(doinit, 1, 0) < 0)
         ;
       if (dial_tty == NULL)
         exit(1);
     }
   }
   else {
-    if (!dosetup && open_term(doinit, 1) < 0)
+    if (!dosetup && open_term(doinit, 1, 0) < 0)
       exit(1);
   }
 
@@ -1305,7 +1305,7 @@
       mc_wclose(stdwin, 1);
       exit(0);
     }
-    while ((dial_tty = get_port(P_PORT)) != NULL && open_term(doinit, 1) < 0)
+    while ((dial_tty = get_port(P_PORT)) != NULL && open_term(doinit, 1, 0) < 0)
       ;
     if (dial_tty == NULL)
       exit(1);
diff -r a041b108875e src/minicom.h
--- a/src/minicom.h	Sun Jun 06 11:52:25 2010 +0200
+++ b/src/minicom.h	Fri Jul 16 10:50:39 2010 +0200
@@ -216,7 +216,7 @@
 char *esc_key(void);
 void term_socket_connect(void);
 void term_socket_close(void);
-int  open_term(int doinit, int show_win_on_error);
+int  open_term(int doinit, int show_win_on_error, int no_msgs);
 void init_emul(int type, int do_init);
 void timer_update(void);
 void mode_status(void);

Adam
-- 
Adam                 adam at os.inf.tu-dresden.de
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/



More information about the minicom-devel mailing list