<commit_before># encoding: utf-8
"""
daemon.py

Created by <NAME> on 2011-11-29.
Copyright (c) 2011 Exa Networks. All rights reserved.
"""

import os
import sys
import pwd
import errno
import socket
import resource

from .log.logger import Logger

def signed (value):
	if value == sys.maxint:
		return -1
	return value

class Daemon (object):
	def __init__ (self,configuration):
		self.daemonize = configuration.daemon.daemonize
		self.user = configuration.daemon.user
		self.log = Logger('daemon', configuration.log.daemon)
		#mask = os.umask(0137)
		if configuration.daemon.filemax:
			try:
				# default on mac are (256,-1)
				resource.setrlimit(resource.RLIMIT_NOFILE, (configuration.daemon.filemax, -1))
			except (resource.error,ValueError),e:
				soft,hard = resource.getrlimit(resource.RLIMIT_NOFILE)
				self.log.error('could not increase file descriptor limit : %s' % str(e))
				self.log.error('the current limit is %d' % signed(soft))
				self.log.error('the maximum possible limit is %d' % signed(hard))

	def drop_privileges (self):
		"""returns true if we are left with insecure privileges"""
		# os.name can be ['posix', 'nt', 'os2', 'ce', 'java', 'riscos']
		if os.name not in ['posix',]:
			return False

		uid = os.getuid()
		gid = os.getgid()

		if uid and gid:
			return False

		try:
			user = pwd.getpwnam(self.user)
			nuid = int(user.pw_uid)
			ngid = int(user.pw_uid)
		except KeyError:
			return True

		# not sure you can change your gid if you do not have a pid of zero
		try:
			if not uid:
				os.setuid(nuid)
			if not gid:
				os.setgid(ngid)
			return False
		except OSError:
			return True

	def _is_socket (self,fd):
		try:
			s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
		except ValueError,e:
			# The file descriptor is closed
			return False
		try:
			s.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)
		except socket.error, e:
			# It is look like one but it is not a socket ...
			if e.args[0] == errno.ENOTSOCK:
				return False
		return True

	def daemonise (self):
		if not self.daemonize:
			return<commit_msg>Set gid before attempting to set gid<commit_after># encoding: utf-8
"""
daemon.py

Created by <NAME> on 2011-11-29.
Copyright (c) 2011 Exa Networks. All rights reserved.
"""

import os
import sys
import pwd
import errno
import socket
import resource

from .log.logger import Logger

def signed (value):
	if value == sys.maxint:
		return -1
	return value

class Daemon (object):
	def __init__ (self,configuration):
		self.daemonize = configuration.daemon.daemonize
		self.user = configuration.daemon.user
		self.log = Logger('daemon', configuration.log.daemon)
		#mask = os.umask(0137)
		if configuration.daemon.filemax:
			try:
				soft_limit,hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
				wanted_limit = min(configuration.daemon.filemax, hard_limit if hard_limit > 0 else 0)

				# default on mac are (256,-1)
				resource.setrlimit(resource.RLIMIT_NOFILE, (wanted_limit, hard_limit))

			except (resource.error,ValueError),e:
				self.log.error('could not increase file descriptor limit : %s' % str(e))

		soft,hard = resource.getrlimit(resource.RLIMIT_NOFILE)
		self.log.error('the current file descriptor limit is %d' % signed(soft))
		self.log.error('the maximum possible file descriptor limit is %d' % signed(hard))
		self.log.error('the requested file descriptor limit was %d' % signed(configuration.daemon.filemax))

		self.file_limit = soft

	def drop_privileges (self):
		"""returns true if we are left with insecure privileges"""
		# os.name can be ['posix', 'nt', 'os2', 'ce', 'java', 'riscos']
		if os.name not in ['posix',]:
			return False

		uid = os.getuid()
		gid = os.getgid()

		if uid and gid:
			return False

		try:
			user = pwd.getpwnam(self.user)
			nuid = int(user.pw_uid)
			ngid = int(user.pw_uid)
		except KeyError:
			return True

		# not sure you can change your gid if you do not have a pid of zero
		try:
			if not gid:
				os.setgid(ngid)
			if not uid:
				os.setuid(nuid)
			return False
		except OSError:
			return True

	def _is_socket (self,fd):
		try:
			s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
		except ValueError,e:
			# The file descriptor is closed
			return False
		try:
			s.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)
		except socket.error, e:
			# It is look like one but it is not a socket ...
			if e.args[0] == errno.ENOTSOCK:
				return False
		return True

	def daemonise (self):
		if not self.daemonize:
			return
{'fraction_non_alphanumeric': 0.11203226529240422, 'fraction_numerical': 0.011427291059825229, 'mean_word_length': 3.5551020408163265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7219547', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1543, 'n_words': 473}