Created
September 4, 2014 09:58
-
-
Save kanru/01c48650ee5541316798 to your computer and use it in GitHub Desktop.
task tracer logserver
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | |
/* vim: set ts=2 et sw=2 tw=80: */ | |
/* This Source Code Form is subject to the terms of the Mozilla Public | |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
#include <errno.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <sys/un.h> | |
#include <unistd.h> | |
#ifdef __ANDROID__ | |
static char LOG_SOCKET_PATH[] = "/data/local/tmp/ttlog_socket"; | |
static char LOG_FILE_PATH[] = "/data/local/tmp/ttlog"; | |
#else | |
static char LOG_SOCKET_PATH[] = "/tmp/ttlog_socket"; | |
static char LOG_FILE_PATH[] = "/tmp/ttlog"; | |
#endif | |
static int quit = 0; | |
void term_handler(int sig) | |
{ | |
quit = 1; | |
} | |
int | |
main(int argc, char* argv[]) | |
{ | |
struct sockaddr_un address; | |
int socket_fd; | |
char* logpath; | |
FILE* logfile; | |
logpath = &LOG_FILE_PATH; | |
if (argc > 1) { | |
logpath = argv[1]; | |
} | |
socket_fd = socket(PF_UNIX, SOCK_DGRAM, 0); | |
if (socket_fd < 0) { | |
perror("logserver:socket"); | |
return 1; | |
} | |
unlink(LOG_SOCKET_PATH); | |
memset(&address, 0, sizeof(struct sockaddr_un)); | |
address.sun_family = AF_UNIX; | |
snprintf(address.sun_path, pathconf("/", _PC_PATH_MAX), LOG_SOCKET_PATH); | |
if (bind(socket_fd, | |
(struct socketaddr*)&address, | |
sizeof(struct sockaddr_un)) != 0) { | |
perror("logserver:bind"); | |
return 1; | |
} | |
size_t retval; | |
char buffer[4096]; | |
logfile = fopen(logpath, "w"); | |
signal(SIGHUP, term_handler); | |
signal(SIGINT, term_handler); | |
signal(SIGKILL, term_handler); | |
signal(SIGPIPE, term_handler); | |
signal(SIGALRM, term_handler); | |
signal(SIGTERM, term_handler); | |
if (!logfile) { | |
perror("logserver:fopen"); | |
return 1; | |
} | |
for (;;) { | |
retval = read(socket_fd, &buffer, sizeof(buffer)); | |
if (retval > 0) { | |
fprintf(logfile, "%s\n", buffer); | |
} | |
if (retval < 0) { | |
perror("logserver:read"); | |
return 1; | |
} | |
if (quit) { | |
break; | |
} | |
} | |
if (errno < 0) { | |
perror("logserver:select"); | |
return 1; | |
} | |
fprintf(stderr, "QUIT\n"); | |
fflush(logfile); | |
fclose(logfile); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment