Make portable to OS X

This commit is contained in:
Barak Michener 2013-08-31 18:10:40 -04:00
parent 5840f7d751
commit 56bb7870d9

28
tpm.c
View file

@ -19,7 +19,10 @@ char* MakeSocketName() {
char* username = getenv("USER"); char* username = getenv("USER");
char* socket_name = (char*) calloc(strlen(username) + strlen(P_tmpdir) + 40, sizeof(char)); char* socket_name = (char*) calloc(strlen(username) + strlen(P_tmpdir) + 40, sizeof(char));
strcat(socket_name, P_tmpdir); strcat(socket_name, P_tmpdir);
strcat(socket_name, "/.tpm-"); if (P_tmpdir[strlen(P_tmpdir) - 1] != '/') {
strcat(socket_name, "/");
}
strcat(socket_name, ".tpm-");
strcat(socket_name, username); strcat(socket_name, username);
return socket_name; return socket_name;
} }
@ -44,7 +47,7 @@ int ClientMain(char* done_message) {
strcpy(remote.sun_path, socket_name); strcpy(remote.sun_path, socket_name);
free(socket_name); free(socket_name);
int len = strlen(remote.sun_path) + sizeof(remote.sun_family); int len = strlen(remote.sun_path) + sizeof(remote.sun_family) + 1;
if (connect(sock_fd, (struct sockaddr *)&remote, len) == -1) { if (connect(sock_fd, (struct sockaddr *)&remote, len) == -1) {
printf("%s\n", done_message); printf("%s\n", done_message);
return 0; return 0;
@ -81,7 +84,7 @@ int DaemonMain(int countdown_time) {
strcpy(local.sun_path, socket_name); strcpy(local.sun_path, socket_name);
unlink(local.sun_path); unlink(local.sun_path);
free(socket_name); free(socket_name);
int len = strlen(local.sun_path) + sizeof(local.sun_family); 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);
@ -160,6 +163,8 @@ int DaemonMain(int countdown_time) {
} }
ok = nanosleep(&ts, NULL); ok = nanosleep(&ts, NULL);
} }
close(sock_fd);
printf("%s\n", local.sun_path);
unlink(local.sun_path); unlink(local.sun_path);
char* post_hook = MakePostHookPath(); char* post_hook = MakePostHookPath();
if (access(post_hook, X_OK) != -1) { if (access(post_hook, X_OK) != -1) {
@ -173,6 +178,9 @@ int main(int argc, char** argv) {
int c; int c;
int countdown_time = kDefaultLengthSecs; int countdown_time = kDefaultLengthSecs;
char* done_message = (char*) kDefaultDoneMessage; char* done_message = (char*) kDefaultDoneMessage;
int got_positional = 0;
char* command = "";
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':
@ -186,12 +194,20 @@ int main(int argc, char** argv) {
break; break;
} }
} }
if (optind < argc) { if (optind < argc && !got_positional) {
char* command = argv[optind]; command = argv[optind];
got_positional = 1;
} else {
break;
}
optind++;
}
if (strcmp(command, "start") == 0) { if (strcmp(command, "start") == 0) {
DaemonMain(countdown_time); DaemonMain(countdown_time);
} }
} else { else {
return ClientMain(done_message); return ClientMain(done_message);
} }
exit(0); exit(0);