reformat
This commit is contained in:
parent
187127a646
commit
b1e4f4e790
1 changed files with 124 additions and 124 deletions
248
tpm.cc
248
tpm.cc
|
|
@ -61,149 +61,149 @@ int ClientMain(string done_message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int DaemonMain(int countdown_time) {
|
int DaemonMain(int countdown_time) {
|
||||||
/* Our process ID and Session ID */
|
/* Our process ID and Session ID */
|
||||||
int ok;
|
int ok;
|
||||||
pid_t pid, sid;
|
pid_t pid, sid;
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
ts.tv_sec = 0;
|
ts.tv_sec = 0;
|
||||||
ts.tv_nsec = 1000 /*micro*/ * 1000 /* milli */ * 50;
|
ts.tv_nsec = 1000 /*micro*/ * 1000 /* milli */ * 50;
|
||||||
|
|
||||||
string socket_name = SocketName();
|
string socket_name = SocketName();
|
||||||
|
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
struct sockaddr_un local, remote;
|
struct sockaddr_un local, remote;
|
||||||
|
|
||||||
if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
local.sun_family = AF_UNIX;
|
local.sun_family = AF_UNIX;
|
||||||
strcpy(local.sun_path, socket_name.c_str());
|
strcpy(local.sun_path, socket_name.c_str());
|
||||||
unlink(local.sun_path);
|
unlink(local.sun_path);
|
||||||
int len = strlen(local.sun_path) + sizeof(local.sun_family) + 1;
|
int len = strlen(local.sun_path) + sizeof(local.sun_family) + 1;
|
||||||
if (bind(sock_fd, (struct sockaddr *)&local, len) == -1) {
|
if (bind(sock_fd, (struct sockaddr *)&local, len) == -1) {
|
||||||
perror("bind");
|
perror("bind");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
int flags = fcntl(sock_fd,F_GETFL,0);
|
int flags = fcntl(sock_fd,F_GETFL,0);
|
||||||
fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);
|
fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
|
||||||
/* Fork off the parent process */
|
/* Fork off the parent process */
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
/* If we got a good PID, then
|
/* If we got a good PID, then
|
||||||
we can exit the parent process. */
|
we can exit the parent process. */
|
||||||
if (pid > 0) {
|
if (pid > 0) {
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Change the file mode mask */
|
/* Change the file mode mask */
|
||||||
umask(0);
|
umask(0);
|
||||||
|
|
||||||
/* Open any logs here */
|
/* Open any logs here */
|
||||||
|
|
||||||
/* Create a new SID for the child process */
|
/* Create a new SID for the child process */
|
||||||
sid = setsid();
|
sid = setsid();
|
||||||
if (sid < 0) {
|
if (sid < 0) {
|
||||||
/* Log the failure */
|
/* Log the failure */
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Change the current working directory */
|
/* Change the current working directory */
|
||||||
if ((chdir("/")) < 0) {
|
if ((chdir("/")) < 0) {
|
||||||
/* Log the failure */
|
/* Log the failure */
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Close out the standard file descriptors */
|
/* Close out the standard file descriptors */
|
||||||
close(STDIN_FILENO);
|
close(STDIN_FILENO);
|
||||||
close(STDOUT_FILENO);
|
close(STDOUT_FILENO);
|
||||||
close(STDERR_FILENO);
|
close(STDERR_FILENO);
|
||||||
|
|
||||||
if (listen(sock_fd, 5) == -1) {
|
if (listen(sock_fd, 5) == -1) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct timeval start_time;
|
struct timeval start_time;
|
||||||
ok = gettimeofday(&start_time, NULL);
|
ok = gettimeofday(&start_time, NULL);
|
||||||
int remote_fd = -1;
|
int remote_fd = -1;
|
||||||
|
|
||||||
bool wakeup = false;
|
bool wakeup = false;
|
||||||
while (!wakeup) {
|
while (!wakeup) {
|
||||||
struct timeval current_time;
|
struct timeval current_time;
|
||||||
struct timeval diff_time;
|
struct timeval diff_time;
|
||||||
ok = gettimeofday(¤t_time, NULL);
|
ok = gettimeofday(¤t_time, NULL);
|
||||||
timersub(¤t_time, &start_time, &diff_time);
|
timersub(¤t_time, &start_time, &diff_time);
|
||||||
if (diff_time.tv_sec >= countdown_time) {
|
if (diff_time.tv_sec >= countdown_time) {
|
||||||
wakeup = true;
|
wakeup = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
long remaining_time = (long)countdown_time - diff_time.tv_sec;
|
long remaining_time = (long)countdown_time - diff_time.tv_sec;
|
||||||
socklen_t t = sizeof(remote);
|
socklen_t t = sizeof(remote);
|
||||||
while ((remote_fd = accept(sock_fd, (struct sockaddr *)&remote, &t)) != -1) {
|
while ((remote_fd = accept(sock_fd, (struct sockaddr *)&remote, &t)) != -1) {
|
||||||
char message[100];
|
char message[100];
|
||||||
int message_len = snprintf(message, 100, "%ld:%02ld", remaining_time / 60, remaining_time % 60);
|
int message_len = snprintf(message, 100, "%ld:%02ld", remaining_time / 60, remaining_time % 60);
|
||||||
if (send(remote_fd, message, message_len, 0) < 0) {
|
if (send(remote_fd, message, message_len, 0) < 0) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
close(remote_fd);
|
close(remote_fd);
|
||||||
}
|
}
|
||||||
// Make sure it was just EWOULDBLOCK
|
// Make sure it was just EWOULDBLOCK
|
||||||
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
ok = nanosleep(&ts, NULL);
|
ok = nanosleep(&ts, NULL);
|
||||||
}
|
}
|
||||||
close(sock_fd);
|
close(sock_fd);
|
||||||
printf("%s\n", local.sun_path);
|
printf("%s\n", local.sun_path);
|
||||||
unlink(local.sun_path);
|
unlink(local.sun_path);
|
||||||
string post_hook = MakePostHookPath();
|
string post_hook = MakePostHookPath();
|
||||||
char* argv[] = {(char *)post_hook.c_str(), NULL};
|
char* argv[] = {(char *)post_hook.c_str(), NULL};
|
||||||
if (access(post_hook.c_str(), X_OK) != -1) {
|
if (access(post_hook.c_str(), X_OK) != -1) {
|
||||||
execv(post_hook.c_str(), argv);
|
execv(post_hook.c_str(), argv);
|
||||||
}
|
}
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
int c;
|
int c;
|
||||||
int countdown_time = kDefaultLengthSecs;
|
int countdown_time = kDefaultLengthSecs;
|
||||||
string done_message = kDefaultDoneMessage;
|
string done_message = kDefaultDoneMessage;
|
||||||
int got_positional = 0;
|
int got_positional = 0;
|
||||||
string command = "";
|
string command = "";
|
||||||
while (1) {
|
while (1) {
|
||||||
while ( (c = getopt(argc, argv, "bs:m:d:")) != -1) {
|
while ( (c = getopt(argc, argv, "bs:m:d:")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 's':
|
case 's':
|
||||||
countdown_time = atoi(optarg);
|
countdown_time = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
countdown_time = atoi(optarg) * 60;
|
countdown_time = atoi(optarg) * 60;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
done_message = optarg;
|
done_message = optarg;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (optind < argc && !got_positional) {
|
|
||||||
command = argv[optind];
|
|
||||||
got_positional = 1;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
optind++;
|
|
||||||
}
|
}
|
||||||
|
if (optind < argc && !got_positional) {
|
||||||
|
command = argv[optind];
|
||||||
|
got_positional = 1;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
optind++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (command == "start") {
|
if (command == "start") {
|
||||||
DaemonMain(countdown_time);
|
DaemonMain(countdown_time);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return ClientMain(done_message);
|
return ClientMain(done_message);
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue