freeing device file handle upon device unplugging?

Alexander Stein alexander.stein at systec-electronic.com
Mon Jul 19 06:43:56 UTC 2010


Hello Adam,

Am Freitag, 16. Juli 2010, 10:51:51 schrieb Adam Lackorzynski:
> 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);

This works great. Even the reconnection runs smoothly. There is just one small 
drawback which might not be solvable: If the message box appears that the 
device can't be opened, it doesn't not get "removed" if there is a 
reconnection. On other words, if there is no new output one doesn't know that 
there is a reconnection.

Best regards,
Alexander



More information about the minicom-devel mailing list