def execute_command(): pipe_fd = posix.pipe() pid = posix.fork() if pid == 0: cmdline = ["lsblk"] if check_fs_val.get() or check_UUID_val.get(): cmdline.append("-o") col = "+" if check_fs_val.get(): col += "fstype" if check_fs_val.get() and check_UUID_val.get(): col += "," if check_UUID_val.get(): col += "UUID" cmdline.append(col) posix.dup2(pipe_fd[1], 1) posix.close(pipe_fd[0]) posix.close(pipe_fd[1]) posix.execv("/bin/lsblk", cmdline) quit() else: posix.close(pipe_fd[1]) ret = bytearray() readbytes = posix.read(pipe_fd[0], 1000) while readbytes != b"": ret += readbytes readbytes = posix.read(pipe_fd[0], 1000) posix.close(pipe_fd[0]) posix.wait() return str(ret, sys.stdout.encoding)
def Wait(self): # This is a list of async jobs while True: try: pid, status = posix.wait() except OSError as e: #log('wait() error: %s', e) if e.errno == errno.ECHILD: return False # nothing to wait for caller should stop elif e.errno == errno.EINTR: # This happens when we register a handler for SIGINT, and thus never # get the KeyboardInterrupt exception? Not sure why. # Try # $ cat # Now hit Ctrl-C #log('Continuing') continue # try again else: # An error we don't know about. raise else: break # no exception thrown, so no need to retry #log('WAIT got %s %s', pid, status) # TODO: change status in more cases. if posix.WIFSIGNALED(status): if posix.WTERMSIG(status) == signal.SIGINT: print() elif posix.WIFEXITED(status): status = posix.WEXITSTATUS(status) #log('exit status: %s', status) # This could happen via coding error. But this may legitimately happen # if a grandchild outlives the child (its parent). Then it is reparented # under this process, so we might receive notification of its exit, even # though we didn't start it. We can't have any knowledge of such # processes, so print a warning. if pid not in self.callbacks: util.warn("PID %d stopped, but osh didn't start it", pid) return True # caller should keep waiting callback = self.callbacks.pop(pid) callback(pid, status) self.last_status = status # for wait -n return True # caller should keep waiting
def master_sig_handler_stop_server(signum, frame): print("Signal {} received, server will be stopped".format(signum)) posix.kill(0, signal.SIGINT) posix.wait() sys.exit(0)