def _init_mailslot(cls, id): if cls.mm is None: # First time has some extra setup, not all vars need to be kept. buf = os.fstat(cls.fd) assert STAT.S_ISREG(buf.st_mode), 'Mailbox FD is not a regular file' cls.mm = mmap.mmap(cls.fd, 0) view = memoryview(cls.mm) mbg = IVSHMSG_MailGlobals.from_buffer(view) # Convenience cls.nClients = mbg.nClients cls.nEvents = mbg.nEvents cls.server_id = mbg.server_id # Create all the peer data structures now as it simplifies # connection logic removes interplay from twisted_client.py. cls.slots = [ None, ] * cls.nEvents cls.slots[0] = mbg for slot in range(1, cls.nEvents): cls.slots[slot] = IVSHMSG_MailSlot.from_buffer( view[mbg.slotsize * slot : mbg.slotsize * (slot + 1)]) assert cls.slots[slot].peer_id == slot, 'What happened?' if id > cls.server_id: # Probably a test run of twisted_restapi return assert cls.slots[id].peer_id == id, 'What happened?' cls.clear_mailslot(id)
def __init__(self, args=None, fd=-1, client_id=-1, nodename=None): '''Server: args with command line stuff from command line. Client: starts with an fd and id read from AF_UNIX socket.''' if self._beentheredonethat: return self.__class__._beetnheredonethat = True assert (self.MS_MSG_off + self.MS_MAX_MSGLEN == self.MAILBOX_SLOTSIZE), 'Fix this NOW' self.filesize = self.MAILBOX_MAX_SLOTS * self.MAILBOX_SLOTSIZE if args is None: assert fd > 0 and client_id > 0 and isinstance(nodename, str), \ 'Bad call, ump!' self.__class__.fd = fd self.nodename = nodename self._init_mailslot(client_id, nodename) return assert fd == -1 and client_id == -1, 'Cannot assign ids to server' path = args.mailbox # Match previously written code gr_gid = -1 # Makes no change. Try Debian, CentOS, other for gr_name in ('libvirt-qemu', 'libvirt', 'libvirtd'): try: gr_gid = grp.getgrnam(gr_name).gr_gid break except Exception as e: pass if '/' not in path: path = '/dev/shm/' + path oldumask = os.umask(0) try: if not os.path.isfile(path): fd = os.open(path, os.O_RDWR | os.O_CREAT, mode=0o666) os.posix_fallocate(fd, 0, self.filesize) os.fchown(fd, -1, gr_gid) else: # Re-condition and re-use lstat = os.lstat(path) assert STAT.S_ISREG(lstat.st_mode), 'not a regular file' assert lstat.st_size >= self.filesize, \ 'existing size (%d) is < required (%d)' % ( lstat.st_size, self.filesize) if lstat.st_gid != gr_gid and gr_gid > 0: print('Changing %s to group %s' % (path, gr_name)) os.chown(path, -1, gr_gid) if lstat.st_mode & 0o660 != 0o660: # at least print('Changing %s to permissions 666' % path) os.chmod(path, 0o666) fd = os.open(path, os.O_RDWR) except Exception as e: raise RuntimeError('Problem with %s: %s' % (path, str(e))) os.umask(oldumask) self.path = path # Final absolute path self.__class__.fd = fd self._initialize_mailbox(args)
def ftype(self): st = lstat(self._path) if statc.S_ISLNK(st.st_mode): return LINK elif statc.S_ISREG(st.st_mode): return FILE elif statc.S_ISDIR(st.st_mode): return DIR else: raise ValueError("%s is not in %s" % (self, TYPES))
def _init_mailslot(cls, id, nodename): buf = os.fstat(cls.fd) assert STAT.S_ISREG(buf.st_mode), 'Mailbox FD is not a regular file' if cls.mm is None: cls.mm = mmap.mmap(cls.fd, 0) (cls.nClients, cls.nEvents, cls.server_id) = struct.unpack( 'QQQ', cls.mm[cls.G_NCLIENTS_off:cls.G_NCLIENTS_off + 24]) # mailbox slot starts with nodename cls.clear_mailslot(id, nodenamebytes=nodename.encode())