2882: arv-mount restores signal handlers after sp.wait(). Fixed spelling ellapsed...
[arvados.git] / services / fuse / bin / arv-mount
1 #!/usr/bin/env python
2
3 from arvados_fuse import *
4 import arvados
5 import subprocess
6 import argparse
7 import daemon
8 import signal
9
10 if __name__ == '__main__':
11     # Handle command line parameters
12     parser = argparse.ArgumentParser(
13         description='''Mount Keep data under the local filesystem.  By default, if neither
14         --collection or --tags is specified, this mounts as a virtual directory
15         under which all Keep collections are available as subdirectories named
16         with the Keep locator; however directories will not be visible to 'ls'
17         until a program tries to access them.''',
18         epilog="""
19 Note: When using the --exec feature, you must either specify the
20 mountpoint before --exec, or mark the end of your --exec arguments
21 with "--".
22 """)
23     parser.add_argument('mountpoint', type=str, help="""Mount point.""")
24     parser.add_argument('--allow-other', action='store_true',
25                         help="""Let other users read the mount""")
26     parser.add_argument('--collection', type=str, help="""Mount only the specified collection at the mount point.""")
27     parser.add_argument('--tags', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing tagged
28 collections on the server.""")
29     parser.add_argument('--groups', action='store_true', help="""Mount as a virtual directory consisting of subdirectories representing groups on the server.""")
30     parser.add_argument('--debug', action='store_true', help="""Debug mode""")
31     parser.add_argument('--foreground', action='store_true', help="""Run in foreground (default is to daemonize unless --exec specified)""", default=False)
32     parser.add_argument('--exec', type=str, nargs=argparse.REMAINDER,
33                         dest="exec_args", metavar=('command', 'args', '...', '--'),
34                         help="""Mount, run a command, then unmount and exit""")
35
36     args = parser.parse_args()
37
38     # Create the request handler
39     operations = Operations(os.getuid(), os.getgid())
40
41     if args.groups:
42         api = arvados.api('v1')
43         e = operations.inodes.add_entry(GroupsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
44     elif args.tags:
45         api = arvados.api('v1')
46         e = operations.inodes.add_entry(TagsDirectory(llfuse.ROOT_INODE, operations.inodes, api))
47     elif args.collection != None:
48         # Set up the request handler with the collection at the root
49         e = operations.inodes.add_entry(CollectionDirectory(llfuse.ROOT_INODE, operations.inodes, args.collection))
50     else:
51         # Set up the request handler with the 'magic directory' at the root
52         operations.inodes.add_entry(MagicDirectory(llfuse.ROOT_INODE, operations.inodes))
53
54     # FUSE options, see mount.fuse(8)
55     opts = [optname for optname in ['allow_other', 'debug']
56             if getattr(args, optname)]
57
58     if args.exec_args:
59         # Initialize the fuse connection
60         llfuse.init(operations, args.mountpoint, opts)
61
62         t = threading.Thread(None, lambda: llfuse.main())
63         t.start()
64
65         # wait until the driver is finished initializing
66         operations.initlock.wait()
67
68         rc = 255
69         try:
70             sp = subprocess.Popen(args.exec_args, shell=False)
71
72             # forward signals to the process.
73             signal.signal(signal.SIGINT, lambda signum, frame: sp.send_signal(signum))
74             signal.signal(signal.SIGTERM, lambda signum, frame: sp.send_signal(signum))
75             signal.signal(signal.SIGQUIT, lambda signum, frame: sp.send_signal(signum))
76
77             # wait for process to complete.
78             rc = sp.wait()
79
80             # restore default signal handlers.
81             signal.signal(signal.SIGINT, signal.SIG_DFL)
82             signal.signal(signal.SIGTERM, signal.SIG_DFL)
83             signal.signal(signal.SIGQUIT, signal.SIG_DFL)
84         except OSError as e:
85             sys.stderr.write('arv-mount: %s -- exec %s\n' % (str(e), args.exec_args))
86             rc = e.errno
87         except Exception as e:
88             sys.stderr.write('arv-mount: %s\n' % str(e))
89         finally:
90             subprocess.call(["fusermount", "-u", "-z", args.mountpoint])
91
92         exit(rc)
93     else:
94         if args.foreground:
95             # Initialize the fuse connection
96             llfuse.init(operations, args.mountpoint, opts)
97             llfuse.main()
98         else:
99             # Initialize the fuse connection
100             llfuse.init(operations, args.mountpoint, opts)
101             with daemon.DaemonContext():
102                 llfuse.main()