Created
July 29, 2015 09:49
-
-
Save danieltroger/63cbbc9bf0130bb52318 to your computer and use it in GitHub Desktop.
Time limit thingy for ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
define("target","daniel"); | |
define("maxtime",60); // per day | |
define("pidfile","/var/run/throwyaoff.pid"); | |
define("timefile","/etc/time"); | |
if(posix_getuid() != 0) die("Run as root" . PHP_EOL); | |
if($argc == 1) die($argv[0] . " <\033[34mcommand\033[0m>" . PHP_EOL . "Where \033[34mcommand\033[0m is one of \033[32mreset\033[0m, \033[32mstart\033[0m, \033[32mstop\033[0m or \033[32mrestart\033[0m." . PHP_EOL); | |
if($argv[1] == "restart") posix_kill(getrunningpid(),SIGUSR2); | |
if($argv[1] == "start" || $argv[1] == "restart") | |
{ | |
$pid = pcntl_fork(); | |
if ($pid == -1) | |
{ | |
die('could not fork'); | |
} | |
else if ($pid) | |
{ | |
exit; | |
// we are the parent | |
//pcntl_wait($status); //Protect against Zombie children | |
} | |
else | |
{ | |
if(is_link(pidfile) && !is_dir(pidfile)) | |
{ | |
echo "Removed old, unused pidfile" . PHP_EOL; | |
unlink(pidfile); | |
} | |
if(is_link(pidfile) && is_dir(pidfile)) die("Already running as PID " . getrunningpid() . PHP_EOL); | |
if (@symlink("/proc/" . posix_getpid(), pidfile) !== FALSE) | |
{ | |
register_shutdown_function('unlink', pidfile); | |
echo "Alright, successfully forked to " . posix_getpid() . " and locked the lockfile." . PHP_EOL; | |
declare(ticks = 1); | |
pcntl_signal(SIGUSR2, "stop"); | |
pcntl_signal(SIGUSR1, "rst"); | |
$alerts = Array(); | |
$ot = file_get_contents(timefile); | |
$tim = fopen(timefile,"w+") or die("Couldn't open timefile for reading and writing" . PHP_EOL); | |
settime($ot); | |
if(filemtime(timefile) < time()-(date("H")*60*60+date("i")*60+date("s"))) rst(); | |
if(gettime() == "") rst(); | |
while(true) | |
{ | |
if(date("Hi") == 0001) rst(); | |
sleep(60); | |
if( | |
explode(" ",explode("\n",shell_exec("DISPLAY=':0' xset -q|grep 'Monitor is'"))[0])[4] == "On" | |
&& in_array(target,explode("\n",shell_exec("ps axu | grep 'gnome-session --session' | grep -v grep | awk ' { print $1 } '")))) | |
{ | |
if(gettime() >= maxtime) throwout(); | |
elseif(gettime() >= maxtime-5) alert(5); | |
elseif(gettime() >= maxtime-15) alert(15); | |
settime(gettime()+1); | |
} | |
} | |
} | |
else | |
{ | |
die("Couldn't lock " . pidfile . PHP_EOL); | |
} | |
} | |
} | |
elseif($argv[1] == "stop") | |
{ | |
posix_kill(getrunningpid(),SIGUSR2); | |
} | |
elseif($argv[1] == "reset") | |
{ | |
posix_kill(getrunningpid(),SIGUSR1); | |
} | |
function getrunningpid() | |
{ | |
return @explode("/",@readlink(pidfile))[2]; | |
} | |
function stop() | |
{ | |
exit; | |
posix_kill(posix_getpid()); | |
} | |
function rst() | |
{ | |
file_put_contents(timefile,0); | |
} | |
function gettime() | |
{ | |
fseek($GLOBALS['tim'],0); | |
$t = (int) fread($GLOBALS['tim'],1024); | |
return $t; | |
} | |
function settime($t) | |
{ | |
fseek($GLOBALS['tim'],0); | |
return fwrite($GLOBALS['tim'],$t); | |
} | |
function throwout() | |
{ | |
shell_exec("su " . target . " -c 'kill -9 -1'"); | |
$alerts = Array(); | |
} | |
function alert($tt) | |
{ | |
if(!in_array($tt,$GLOBALS['alerts'])) | |
{ | |
$GLOBALS['alerts'][] = $tt; | |
$ti = maxtime-gettime(); | |
shell_exec("zenity --warning --title Throwhimhoff --text '<span foreground=\"red\" font=\"32\">Abmeldung in (ca) {$ti} Minuten!</span>' > /dev/null 2>&1 &"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment