nsmartt

py-suspend

Apr 15th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from os import kill
  3. from signal import SIGKILL
  4. from subprocess import Popen, PIPE, STDOUT
  5. from sys import argv, exit
  6. from time import sleep
  7.  
  8. """
  9. Newton Smartt
  10. April 15, 2012
  11.  
  12. Written for use on Arch Linux. It should be simple to modify it for your purposes.
  13. It's written for python 2.7. It must be run as root.
  14. I added the following to my sudoers file:
  15. %wheel ALL=NOPASSWD:/usr/bin/py-suspend
  16. In my case, wheel is the sudoers usergroup, and /usr/bin/py-suspend is the path to this script. In case it isn't clear, this allows any user in the wheel usergroup to run the script using sudo with no password.
  17.  
  18. 1. Find and kill tty1-6
  19. 2. Execute slimlock (as a normal user (user's home must be in /home/)
  20. 3. Hibernate (pm-hibernate) (if argv[1] == 'hibernate') or Suspend (pm-suspend) (if argv[1] == 'suspend')
  21. """
  22.  
  23. def slimlock():
  24.   # Get all users with a home in /home/
  25.   # Then make sure their shell is not /bin/false
  26.   users = []
  27.   lines = open('/etc/passwd', 'r').read().strip().split("\n");
  28.   for line in lines:
  29.     line = line.split(':')
  30.     if line[5][0:6] == "/home/" and line[6] != "/bin/false":
  31.       users.append(line[0])
  32.  
  33.   # Attempt to execute slimlock as each found user
  34.   for user in users:
  35.     cmd = 'sudo -u %s slimlock' % (user,)
  36.     Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
  37.  
  38.   # Sleep for 5 seconds to give slimlock a chance to execute
  39.   # Check running processes for slimlock
  40.   # Join the last, second to last, and 4th to last strings after line.split()
  41.   # "sudo -u slimlock" vs "sudo -u NAMEHERE slimlock"
  42.   # If successful, return True, else False
  43.   sleep(5)
  44.   p = Popen('ps aux | grep slimlock', shell=True, stdout=PIPE, stderr=STDOUT)
  45.   lines = p.stdout.readlines()
  46.   for line in lines:
  47.     split = line.split()
  48.     end = " ".join([split[-4], split[-3], split[-1]])
  49.    
  50.     if end == "sudo -u slimlock":
  51.       return True
  52.   return False
  53.  
  54. # Find tty1-6 and kill them
  55. p = Popen('ps aux | grep tty', shell=True, stdout=PIPE, stderr=STDOUT)
  56.  
  57. for line in p.stdout.readlines():
  58.   fields = line.split()
  59.   owner, pid, tname = fields[0], fields[1], fields[6]
  60.   if len(tname) == 4 and tname[0:3] == 'tty':
  61.     if int(tname[3]) < 7:
  62.       try:
  63.         kill(int(pid), SIGKILL)
  64.       except:
  65.         print "Error: Could not kill %s {'pid': %s, 'user': %s}" % (tname, pid, owner)
  66.  
  67. # Call the slimlock function, check for success, exit if no success.
  68. # We don't want to hibernate or any such thing if slimlock did not run.
  69. if not slimlock():
  70.   print "Could not run slimlock"
  71.   exit(1);
  72.  
  73. # Any additional commands should be appended.
  74. # All will run and wait for the previous command to finish
  75. cmds = []
  76.  
  77. if len(argv) == 2:
  78.   if argv[1] == 'hibernate':
  79.     cmds.append('pm-hibernate')
  80.   elif argv[1] == 'suspend':
  81.     cmds.append('pm-suspend')
  82.  
  83. for cmd in cmds:
  84.   p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
  85.   p.stdout.readlines()
Advertisement
Add Comment
Please, Sign In to add comment