我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.getgid()。
def check_enableusersite(): """Check if user site directory is safe for inclusion The function tests for the command line flag (including environment var), process uid/gid equal to effective uid/gid. None: Disabled for security reasons False: Disabled by user (command line option) True: Safe and enabled """ if hasattr(sys, 'flags') and getattr(sys.flags, 'no_user_site', False): return False if hasattr(os, "getuid") and hasattr(os, "geteuid"): # check process uid == effective uid if os.geteuid() != os.getuid(): return None if hasattr(os, "getgid") and hasattr(os, "getegid"): # check process gid == effective gid if os.getegid() != os.getgid(): return None return True
def check_enableusersite(): """Check if user site directory is safe for inclusion The function tests for the command line flag (including environment var), process uid/gid equal to effective uid/gid. None: Disabled for security reasons False: Disabled by user (command line option) True: Safe and enabled """ if sys.flags.no_user_site: return False if hasattr(os, "getuid") and hasattr(os, "geteuid"): # check process uid == effective uid if os.geteuid() != os.getuid(): return None if hasattr(os, "getgid") and hasattr(os, "getegid"): # check process gid == effective gid if os.getegid() != os.getgid(): return None return True
def startedConnecting(self, connector): fd = connector.transport.fileno() stats = os.fstat(fd) try: filestats = os.stat(connector.transport.addr) except: connector.stopConnecting() return if stat.S_IMODE(filestats[0]) != 0600: log.msg("socket mode is not 0600: %s" % oct(stat.S_IMODE(stats[0]))) elif filestats[4] != os.getuid(): log.msg("socket not owned by us: %s" % stats[4]) elif filestats[5] != os.getgid(): log.msg("socket not owned by our group: %s" % stats[5]) # XXX reenable this when i can fix it for cygwin #elif filestats[-3:] != stats[-3:]: # log.msg("socket doesn't have same create times") else: log.msg('conecting OK') return connector.stopConnecting()
def get_os_info(): """ Get some OS info with psutils and humanfriendly. """ is_win = lambda: True if os.name == 'nt' else False pid = os.getgid() if not is_win() else None ppid = os.getppid() now = time.time() current_process = psutil.Process(pid=ppid) process_uptime = current_process.create_time() process_uptime_delta = now - process_uptime process_uptime_human = humanfriendly.format_timespan(process_uptime_delta) system_uptime = psutil.boot_time() system_uptime_delta = now - system_uptime system_uptime_human = humanfriendly.format_timespan(system_uptime_delta) free_memory = psutil.disk_usage('/').free total_memory = psutil.disk_usage('/').total percent_used_memory = psutil.disk_usage('/').percent used_memory = psutil.disk_usage('/').used free_memory_human = humanfriendly.format_size(free_memory) return vars()
def effectivelyReadable(self): uid = os.getuid() euid = os.geteuid() gid = os.getgid() egid = os.getegid() # This is probably true most of the time, so just let os.access() # handle it. Avoids potential bugs in the rest of this function. if uid == euid and gid == egid: return os.access(self.name, os.R_OK) st = os.stat(self.name) # This may be wrong depending on the semantics of your OS. # i.e. if the file is -------r--, does the owner have access or not? if st.st_uid == euid: return st.st_mode & stat.S_IRUSR != 0 # See comment for UID check above. groups = os.getgroups() if st.st_gid == egid or st.st_gid in groups: return st.st_mode & stat.S_IRGRP != 0 return st.st_mode & stat.S_IROTH != 0
def chown(self, cpioinfo, cpiogetpath): """Set owner of cpiogetpath according to cpioinfo. """ if PWD and hasattr(os, "geteuid") and os.geteuid() == 0: # We have to be root to do so. try: g = GRP.getgrgid(cpioinfo.gid)[2] except KeyError: g = os.getgid() try: u = PWD.getpwuid(cpioinfo.uid)[2] except KeyError: u = os.getuid() try: if cpioinfo.issym() and hasattr(os, "lchown"): os.lchown(cpiogetpath, u, g) else: if sys.platform != "os2emx": os.chown(cpiogetpath, u, g) except EnvironmentError: raise ExtractError("could not change owner")
def getattr(self, path, fh=None): stat = self.send_cmd(cmd='stat', path=path) if stat['status'] != 'ok': raise FuseOSError(ENOENT) fuse_stat = {} # Set it to 777 access fuse_stat['st_mode'] = 0 if stat['type'] == 'folder': fuse_stat['st_mode'] |= S_IFDIR else: fuse_stat['st_mode'] |= S_IFREG fuse_stat['st_size'] = stat.get('size', 0) fuse_stat['st_ctime'] = dateparse(stat['created']).timestamp() # TODO change to local timezone fuse_stat['st_mtime'] = dateparse(stat['updated']).timestamp() fuse_stat['st_atime'] = dateparse(stat['updated']).timestamp() # TODO not supported ? fuse_stat['st_mode'] |= S_IRWXU | S_IRWXG | S_IRWXO fuse_stat['st_nlink'] = 1 fuse_stat['st_uid'] = os.getuid() fuse_stat['st_gid'] = os.getgid() return fuse_stat
def _test_all_chown_common(self, chown_func, first_param): """Common code for chown, fchown and lchown tests.""" if os.getuid() == 0: try: # Many linux distros have a nfsnobody user as MAX_UID-2 # that makes a good test case for signedness issues. # http://bugs.python.org/issue1747858 # This part of the test only runs when run as root. # Only scary people run their tests as root. ent = pwd.getpwnam('nfsnobody') chown_func(first_param, ent.pw_uid, ent.pw_gid) except KeyError: pass else: # non-root cannot chown to root, raises OSError self.assertRaises(OSError, chown_func, first_param, 0, 0) # test a successful chown call chown_func(first_param, os.getuid(), os.getgid())
def collect(self, metrics): LOG.debug("CollectMetrics called") for metric in metrics: switch = { "float64": random.random(), "string": "bah", "int64": random.randint( metric.config["int_min"], metric.config["int_max"] ), "other_value": self._args.some_value, "*": None } typ = metric.namespace[2].value if typ == "*": metric.namespace[2].value = str(os.getpid()) if metric.namespace[3].value == "uid": metric.data = os.getuid() elif metric.namespace[3].value == "gid": metric.data = os.getgid() else: metric.data = switch[typ] metric.timestamp = time.time() return metrics