From b5d3dbd699f61429c9da5503cc1456e51c61fb9d Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Sun, 1 Sep 2013 15:24:09 -0400 Subject: [PATCH] move --- tpm-post.sh.example | 2 - tpm.cc | 209 --------------------------------------------------- tpom-post.sh.example | 2 + tpom.cc | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 211 insertions(+), 211 deletions(-) delete mode 100755 tpm-post.sh.example delete mode 100644 tpm.cc create mode 100755 tpom-post.sh.example create mode 100644 tpom.cc diff --git a/tpm-post.sh.example b/tpm-post.sh.example deleted file mode 100755 index aaf15fc..0000000 --- a/tpm-post.sh.example +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -notify-send -a "Pomodoro" -u critical "Pomodoro: Time is up" -t 10000 diff --git a/tpm.cc b/tpm.cc deleted file mode 100644 index 8fac922..0000000 --- a/tpm.cc +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -using std::string; -const int kDefaultLengthSecs = 25 * 60; -const char* kDefaultDoneMessage = ""; - -string SocketName() { - string socket_name; - socket_name.append(P_tmpdir); - socket_name.append("/.tpm-"); - socket_name.append(getenv("USER")); - return socket_name; -} - -string MakePostHookPath() { - string post_hook; - post_hook = getenv("HOME"); - post_hook += "/.tpm-post.sh"; - return post_hook; -} - -int ClientMain(string done_message) { - int sock_fd; - struct sockaddr_un remote; - if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { - perror("socket"); - exit(1); - } - remote.sun_family = AF_UNIX; - string socket_name = SocketName(); - strcpy(remote.sun_path, socket_name.c_str()); - - int len = strlen(remote.sun_path) + sizeof(remote.sun_family) + 1; - if (connect(sock_fd, (struct sockaddr *)&remote, len) == -1) { - printf("%s\n", done_message.c_str()); - return 0; - } - - int recv_length; - char buf[100]; - if ((recv_length = recv(sock_fd, buf, 100, 0)) > 0) { - buf[recv_length] = '\0'; - printf("%s\n", buf); - } - close(sock_fd); - return 0; -} - -int DaemonMain(int countdown_time) { - /* Our process ID and Session ID */ - int ok; - pid_t pid, sid; - struct timespec ts; - ts.tv_sec = 0; - ts.tv_nsec = 1000 /*micro*/ * 1000 /* milli */ * 50; - - string socket_name = SocketName(); - - int sock_fd; - struct sockaddr_un local, remote; - - if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { - perror("socket"); - exit(1); - } - local.sun_family = AF_UNIX; - strcpy(local.sun_path, socket_name.c_str()); - unlink(local.sun_path); - int len = strlen(local.sun_path) + sizeof(local.sun_family) + 1; - if (bind(sock_fd, (struct sockaddr *)&local, len) == -1) { - perror("bind"); - exit(1); - } - int flags = fcntl(sock_fd,F_GETFL,0); - fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK); - - /* Fork off the parent process */ - pid = fork(); - if (pid < 0) { - exit(EXIT_FAILURE); - } - /* If we got a good PID, then - we can exit the parent process. */ - if (pid > 0) { - exit(EXIT_SUCCESS); - } - - /* Change the file mode mask */ - umask(0); - - /* Open any logs here */ - - /* Create a new SID for the child process */ - sid = setsid(); - if (sid < 0) { - /* Log the failure */ - exit(EXIT_FAILURE); - } - - - /* Change the current working directory */ - if ((chdir("/")) < 0) { - /* Log the failure */ - exit(EXIT_FAILURE); - } - - /* Close out the standard file descriptors */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - - if (listen(sock_fd, 5) == -1) { - exit(1); - } - - - struct timeval start_time; - ok = gettimeofday(&start_time, NULL); - int remote_fd = -1; - - bool wakeup = false; - while (!wakeup) { - struct timeval current_time; - struct timeval diff_time; - ok = gettimeofday(¤t_time, NULL); - timersub(¤t_time, &start_time, &diff_time); - if (diff_time.tv_sec >= countdown_time) { - wakeup = true; - continue; - } - long remaining_time = (long)countdown_time - diff_time.tv_sec; - socklen_t t = sizeof(remote); - while ((remote_fd = accept(sock_fd, (struct sockaddr *)&remote, &t)) != -1) { - char message[100]; - int message_len = snprintf(message, 100, "%ld:%02ld", remaining_time / 60, remaining_time % 60); - if (send(remote_fd, message, message_len, 0) < 0) { - exit(1); - } - close(remote_fd); - } - // Make sure it was just EWOULDBLOCK - if (errno != EAGAIN && errno != EWOULDBLOCK) { - exit(1); - } - ok = nanosleep(&ts, NULL); - } - close(sock_fd); - printf("%s\n", local.sun_path); - unlink(local.sun_path); - string post_hook = MakePostHookPath(); - char* argv[] = {(char *)post_hook.c_str(), NULL}; - if (access(post_hook.c_str(), X_OK) != -1) { - execv(post_hook.c_str(), argv); - } - exit(EXIT_SUCCESS); -} - -int main(int argc, char** argv) { - int c; - int countdown_time = kDefaultLengthSecs; - string done_message = kDefaultDoneMessage; - int got_positional = 0; - string command = ""; - while (1) { - while ( (c = getopt(argc, argv, "bs:m:d:")) != -1) { - switch (c) { - case 's': - countdown_time = atoi(optarg); - break; - case 'm': - countdown_time = atoi(optarg) * 60; - break; - case 'd': - done_message = optarg; - break; - } - } - if (optind < argc && !got_positional) { - command = argv[optind]; - got_positional = 1; - } else { - break; - } - optind++; - } - - - if (command == "start") { - DaemonMain(countdown_time); - } - else { - return ClientMain(done_message); - } - exit(0); -} diff --git a/tpom-post.sh.example b/tpom-post.sh.example new file mode 100755 index 0000000..aaf15fc --- /dev/null +++ b/tpom-post.sh.example @@ -0,0 +1,2 @@ +#!/bin/bash +notify-send -a "Pomodoro" -u critical "Pomodoro: Time is up" -t 10000 diff --git a/tpom.cc b/tpom.cc new file mode 100644 index 0000000..8fac922 --- /dev/null +++ b/tpom.cc @@ -0,0 +1,209 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +using std::string; +const int kDefaultLengthSecs = 25 * 60; +const char* kDefaultDoneMessage = ""; + +string SocketName() { + string socket_name; + socket_name.append(P_tmpdir); + socket_name.append("/.tpm-"); + socket_name.append(getenv("USER")); + return socket_name; +} + +string MakePostHookPath() { + string post_hook; + post_hook = getenv("HOME"); + post_hook += "/.tpm-post.sh"; + return post_hook; +} + +int ClientMain(string done_message) { + int sock_fd; + struct sockaddr_un remote; + if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + perror("socket"); + exit(1); + } + remote.sun_family = AF_UNIX; + string socket_name = SocketName(); + strcpy(remote.sun_path, socket_name.c_str()); + + int len = strlen(remote.sun_path) + sizeof(remote.sun_family) + 1; + if (connect(sock_fd, (struct sockaddr *)&remote, len) == -1) { + printf("%s\n", done_message.c_str()); + return 0; + } + + int recv_length; + char buf[100]; + if ((recv_length = recv(sock_fd, buf, 100, 0)) > 0) { + buf[recv_length] = '\0'; + printf("%s\n", buf); + } + close(sock_fd); + return 0; +} + +int DaemonMain(int countdown_time) { + /* Our process ID and Session ID */ + int ok; + pid_t pid, sid; + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 1000 /*micro*/ * 1000 /* milli */ * 50; + + string socket_name = SocketName(); + + int sock_fd; + struct sockaddr_un local, remote; + + if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + perror("socket"); + exit(1); + } + local.sun_family = AF_UNIX; + strcpy(local.sun_path, socket_name.c_str()); + unlink(local.sun_path); + int len = strlen(local.sun_path) + sizeof(local.sun_family) + 1; + if (bind(sock_fd, (struct sockaddr *)&local, len) == -1) { + perror("bind"); + exit(1); + } + int flags = fcntl(sock_fd,F_GETFL,0); + fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK); + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) { + exit(EXIT_FAILURE); + } + /* If we got a good PID, then + we can exit the parent process. */ + if (pid > 0) { + exit(EXIT_SUCCESS); + } + + /* Change the file mode mask */ + umask(0); + + /* Open any logs here */ + + /* Create a new SID for the child process */ + sid = setsid(); + if (sid < 0) { + /* Log the failure */ + exit(EXIT_FAILURE); + } + + + /* Change the current working directory */ + if ((chdir("/")) < 0) { + /* Log the failure */ + exit(EXIT_FAILURE); + } + + /* Close out the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + if (listen(sock_fd, 5) == -1) { + exit(1); + } + + + struct timeval start_time; + ok = gettimeofday(&start_time, NULL); + int remote_fd = -1; + + bool wakeup = false; + while (!wakeup) { + struct timeval current_time; + struct timeval diff_time; + ok = gettimeofday(¤t_time, NULL); + timersub(¤t_time, &start_time, &diff_time); + if (diff_time.tv_sec >= countdown_time) { + wakeup = true; + continue; + } + long remaining_time = (long)countdown_time - diff_time.tv_sec; + socklen_t t = sizeof(remote); + while ((remote_fd = accept(sock_fd, (struct sockaddr *)&remote, &t)) != -1) { + char message[100]; + int message_len = snprintf(message, 100, "%ld:%02ld", remaining_time / 60, remaining_time % 60); + if (send(remote_fd, message, message_len, 0) < 0) { + exit(1); + } + close(remote_fd); + } + // Make sure it was just EWOULDBLOCK + if (errno != EAGAIN && errno != EWOULDBLOCK) { + exit(1); + } + ok = nanosleep(&ts, NULL); + } + close(sock_fd); + printf("%s\n", local.sun_path); + unlink(local.sun_path); + string post_hook = MakePostHookPath(); + char* argv[] = {(char *)post_hook.c_str(), NULL}; + if (access(post_hook.c_str(), X_OK) != -1) { + execv(post_hook.c_str(), argv); + } + exit(EXIT_SUCCESS); +} + +int main(int argc, char** argv) { + int c; + int countdown_time = kDefaultLengthSecs; + string done_message = kDefaultDoneMessage; + int got_positional = 0; + string command = ""; + while (1) { + while ( (c = getopt(argc, argv, "bs:m:d:")) != -1) { + switch (c) { + case 's': + countdown_time = atoi(optarg); + break; + case 'm': + countdown_time = atoi(optarg) * 60; + break; + case 'd': + done_message = optarg; + break; + } + } + if (optind < argc && !got_positional) { + command = argv[optind]; + got_positional = 1; + } else { + break; + } + optind++; + } + + + if (command == "start") { + DaemonMain(countdown_time); + } + else { + return ClientMain(done_message); + } + exit(0); +}