Projects STRLCPY criu Commits 33dd66c6
🤬
  • criu-ns: make --pidfile option show pid in caller pidns

    Using the fact that we know criu_pid and criu is a parent of restored
    process we can create pidfile with pid on caller pidns level.
    
    We need to move mount namespace creation to child so that criu-ns can
    see caller pidns proc.
    
    Signed-off-by: Pavel Tikhomirov <[email protected]>
  • Loading...
  • Pavel Tikhomirov committed with Andrei Vagin 1 year ago
    33dd66c6
    1 parent 166aac0f
  • ■ ■ ■ ■ ■ ■
    scripts/criu-ns
    skipped 5 lines
    6 6  import os
    7 7  import fcntl
    8 8  import termios
     9 +import time
    9 10   
    10 11  # <sched.h> constants for unshare
    11 12  CLONE_NEWNS = 0x00020000
    skipped 98 lines
    110 111   if '--restore-sibling' in restore_args:
    111 112   raise OSError(errno.EINVAL, "--restore-sibling is not supported")
    112 113   
    113  - # Unshare pid and mount namespaces
    114  - if _unshare(CLONE_NEWNS | CLONE_NEWPID) != 0:
     114 + # Unshare pid namespace
     115 + if _unshare(CLONE_NEWPID) != 0:
    115 116   _errno = ctypes.get_errno()
    116 117   raise OSError(_errno, errno.errorcode[_errno])
    117 118   
    skipped 5 lines
    123 124   restore_detached = True
    124 125   restore_args.remove('--restore-detached')
    125 126   
     127 + restore_pidfile = None
     128 + if '--pidfile' in restore_args:
     129 + try:
     130 + opt_index = restore_args.index('--pidfile')
     131 + restore_pidfile = restore_args[opt_index + 1]
     132 + del restore_args[opt_index:opt_index + 2]
     133 + except (ValueError, IndexError, FileNotFoundError):
     134 + raise OSError(errno.ENOENT, "--pidfile missing argument")
     135 + 
     136 + if not restore_pidfile.startswith('/'):
     137 + for base_dir_opt in ['--work-dir', '-W', '--images-dir', '-D']:
     138 + if base_dir_opt in restore_args:
     139 + try:
     140 + opt_index = restore_args.index(base_dir_opt)
     141 + restore_pidfile = os.path.join(restore_args[opt_index + 1], restore_pidfile)
     142 + break
     143 + except (ValueError, IndexError, FileNotFoundError):
     144 + raise OSError(errno.ENOENT, base_dir_opt + " missing argument")
     145 + 
    126 146   criu_pid = os.fork()
    127 147   if criu_pid == 0:
     148 + # Unshare mount namespace
     149 + if _unshare(CLONE_NEWNS) != 0:
     150 + _errno = ctypes.get_errno()
     151 + raise OSError(_errno, errno.errorcode[_errno])
     152 + 
    128 153   os.setsid()
    129 154   # Set stdin tty to be a controlling tty of our new session, this is
    130 155   # required by --shell-job option, as for it CRIU would try to set a
    skipped 7 lines
    138 163   
    139 164   _mount_new_proc()
    140 165   run_criu(restore_args)
     166 + 
     167 + if restore_pidfile:
     168 + restored_pid = None
     169 + retry = 5
     170 + 
     171 + while not restored_pid and retry:
     172 + with open('/proc/%d/task/%d/children' % (criu_pid, criu_pid)) as f:
     173 + line = f.readline().strip()
     174 + if len(line):
     175 + restored_pid = line
     176 + break
     177 + retry -= 1
     178 + time.sleep(1)
     179 + 
     180 + if restored_pid:
     181 + with open(restore_pidfile, 'w+') as f:
     182 + f.write(restored_pid)
     183 + else:
     184 + print("Warn: Search of restored pid for --pidfile option timeouted")
    141 185   
    142 186   if restore_detached:
    143 187   return 0
    skipped 131 lines
Please wait...
Page is in error, reload to recover