Subject: x2gocdmanager failing in a number of corner cases

From: Arne Wichmann <[email-masked]>
Date: Fri, 29 Apr 2016 16:50:44 +0200

Package: x2gocdmanager
Version: 1.1.0.2-0x2go1+git20150607.485+jessie.main.1
Tags: patch

I found a number of cases where x2gocdmanager was not mounting/unmounting
CDROMs as expected. Namely:
- When CDs were changed quickly, the change of CD was not detected and the
  old CD stayed mounted
- Sometimes the umount failed because some files were still open (and
  similar cases). In that case x2gocdmanager considered the CD umounted and
  all further tries to mount a CD failed.

I append a patch which apart from these problem cases
- creates support for a log file, so problems can be more easily debugged.
  If the log file is not present a warning will be printed, but
  x2gocdmanager will work without logging as it was before
- makes an effort to unmount everything in /media at the start of the
  manager
- includes some basic debug output so that further problems can be
  pinpointed more easily

Indentations are a bit broken in the patch...

cu

AW

--- /usr/sbin/x2gocdmanager.dpkg-dist   2016-04-29 16:01:19.000000000 +0200
+++ /usr/sbin/x2gocdmanager     2016-04-29 16:01:41.000000000 +0200
@@ -46,11 +46,19 @@
 
        # silence this daemon script completely...
        close(STDIN);
-       close(STDOUT);
-       close(STDERR);
 
        $SIG{TERM}=\&catch_term;
 
+       # do not die here - file unwritable is a way to silence this
+       open(STDOUT,">>/var/log/x2gocdmanager") || warn "open failed: $!";
+       open(STDERR,">>&STDOUT") || print("open failed: $!");
+       print("Started ",time(),"\n");
+
+       # unmount everything which is mounted in /media
+       for (glob("/media/*")) {
+         system("umount -ff $_") or warn "could not umount $_";
+       }
+
        my @mounted;
        my @idedrives;
 
@@ -108,7 +116,11 @@
                        }
                        elsif($rez==101) 
                        {
-                               push(@cdarray,@drives[$i]);
+                               # do not push if media change detected
+                               # this will umount the CD. after the next
+                               # iteration the new one will be mounted
+                               $_=ioctl(CDROM,0x5325,0);
+                               push(@cdarray,@drives[$i]) unless $_==1;
                        }
                        close (CDROM);
                }
@@ -129,33 +141,35 @@
                                open (D,">",expand_filename("~x2gothinclient/export/$name.unexport"));
                                print D "unexport=/media/$name\n";
                                close(D);
-                               system("umount -ff /media/$name");
-
+                               print("umounting $name\n");
+                               if (system("umount -ff /media/$name")==0) {
                                system("killall x2goejectcd");
                                splice(@mounted,$i,1);
                        }
                }
+               }
 
                # enumerate all available CD/DVD devices
                for my $i (0..$#cdarray)
                {
                        # mount the CD/DVD device if not yet mounted...
                        my @arr=grep { "$_" eq "@cdarray[$i]" } @mounted;
-                       if (@arr == 0)
-                       {
+                 if (@arr == 0) {
                                my @name=split("/","@cdarray[$i]");
                                my $name="CDROM-@name[2]";
                                mkdir("/media");
                                mkdir("/media/$name");
+                   print("mounting @cdarray[$i]\n");
                                {
                                        open (D,">",expand_filename("~x2gothinclient/export/$name"));
                                        print D "export=/media/$name\n";
                                        close(D);
+                     print("mount worked: @cdarray[$i]\n");
                                        push (@mounted,@cdarray[$i]);
                                        system("killall x2goejectcd");
                                        system("DISPLAY=:0 x2goejectcd&");
-                               }
+                   } else { warn("mount failed: $?"); }
                        }
                }
        }

-- 
[...] If you don't want to be restricted, don't agree to it. If you are
coerced, comply as much as you must to protect yourself, just don't support
it. Noone can free you but yourself. (crag, on Debian Planet)
Arne Wichmann ([email-masked])