Skip to content

Instantly share code, notes, and snippets.

@mgregoro
Created May 31, 2017 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgregoro/7083da97ebbae9e0648d13bd5db2c671 to your computer and use it in GitHub Desktop.
Save mgregoro/7083da97ebbae9e0648d13bd5db2c671 to your computer and use it in GitHub Desktop.
stops scdaemon politely then rudely.
#!/usr/bin/env perl
use strict;
use v5.10;
use Time::HiRes;
use subs qw/scdaemon_pid/;
$SIG{ALRM} = \&force_kill;
if (my $pid = scdaemon_pid) {
say "[stop_scdaemon] gpg smartcard daemon running, asking it nicely to die";
alarm(5);
system("gpg-connect-agent", "SCD KILLSCD", "SCD BYE", "/bye");
Time::HiRes::sleep(0.10);
force_kill();
} else {
say "[stop_scdaemon] scdaemon wasn't running";
exit;
}
if (my $pid = scdaemon_pid) {
say "[oops] doesn't look like i was able to stop the scdaemon. might have to reboot.";
} else {
say "[stop_scdaemon] scdaemon stopped, its exclusive lock on your smart card shoudln't";
say " bother you anymore. at least until you use it with gpg again.";
}
sub scdaemon_pid {
chomp(my $pid = `ps -ef | grep " scdaemon" | grep -v grep | awk {'print \$2'}`);
return $pid;
}
sub force_kill {
if (my $pid = scdaemon_pid) {
say "[stop_scdaemon] gpg smartcard daemon didn't listen to our nice request so kill -9 ing";
kill('KILL', $pid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment