#!/usr/bin/python -O # Distributed under the terms of the GNU General Public License v2 #UDev Device Helper #Changes : # fixed permissions problem and misc tweaks import sys,os,grp,pwd,commands,stat default_perm = '660' loaded_modules = commands.getoutput("lsmod | awk '{print $1'}").splitlines()[1:] loaded_modules.append('none'); def parse_config(conf_file='udh.conf'): if not os.access(conf_file,os.F_OK) and len(sys.argv) > 1 and os.access(sys.argv[1], os.F_OK) : conf_file = sys.argv[1] f = file(conf_file,"r"); lines = f.readlines(); f.close() final_conf = [] for line in lines: if not line.startswith('#'): conf = line.strip().split(',') temp_dict = {} for c in conf: cc = c.strip().split('=') temp_dict[cc[0].strip()] = cc[1].strip() final_conf.append(temp_dict) return final_conf conf = parse_config() for line in conf: try: if 'kernel_module' in line and line['kernel_module'] in loaded_modules and not os.access(line['dev'],os.F_OK): print "Making node : " + line['dev'] if 'perm' in line : line['perm'] = default_perm if 'dir' in line : print "\t Making Directory : " + line['dir'] + ' ' + os.mkdir(line['dir']) n = line['node'].split(' ') os.mknod(line['dev'], stat.S_IFCHR, os.makedev( int(n[0]), int(n[1]) ) ) #fix this... commands.getoutput("chmod %(p)s %(f)s" % {'p':line['perm'], 'f': line['dev']} ) #commands.getoutput( 'mknod -m '+line['perm']+' '+line['dev']+' c '+line['node']) gid , uid = 0 , 0 if 'group' in line : gid = grp.getgrnam(line['group'])[2] if 'user' in line : uid = pwd.getpwnam(line['user'])[2] os.chown(line['dev'], uid, gid) if 'symlink' in line : print "\tCreating Symlink from %(src)s to %(dst)s" % {'src':line['dev'], 'dst':line['symlink']} os.symlink(line['dev'], line['symlink']) else: print 'Error : [%(e)s] isn\'t currently loaded or device already exists.' % {'e':line["kernel_module"]} except: print "Configuration : "+ str(line)+" is invalid, please check your syntax!"