From: Matteo Nastasi Date: Sat, 13 Aug 2011 13:56:44 +0000 (+0200) Subject: First git commit X-Git-Url: https://mop.ddnsfree.com/gitweb/?p=howmuchuseit.git;a=commitdiff_plain;h=cab5b3f450a3d30ad0d7956f86432418ea7aeccd First git commit --- cab5b3f450a3d30ad0d7956f86432418ea7aeccd diff --git a/howmucheck.c b/howmucheck.c new file mode 100644 index 0000000..c90dc8a --- /dev/null +++ b/howmucheck.c @@ -0,0 +1,68 @@ +#define _XOPEN_SOURCE /* glibc2 needs this */ +#include +#include + +#include "howmuchuseit.h" + +int main(int argc, char *argv[]) +{ + FILE *fp; + struct tm *tmt; + int delta, cur_d = 0; + char bf[1024], tms[1024]; + int len; + long sum, dt, h, m, s; + + delta = (24 * 3600) - atoi(argv[1]) * 3600; + fp = stdin; + + while (fgets(bf, 1024, fp)) { + if (bf[0] == '#') + continue; + sscanf(bf, "%ld:%d", &dt, &len); + if (((dt + delta) / (24 * 3600)) != cur_d) { + if (cur_d > 0) { + time_t cur_t; + cur_t = (time_t)(cur_d * (24 * 3600) - delta); + tmt = localtime((time_t *)&cur_t); + strftime(tms, 1024, "%Y-%m-%d", tmt); + h = sum / 3600; + m = (sum - (h * 3600)) / 60; + s = (sum - (h * 3600) - (m * 60)); + printf(" %s | %02ld:%02ld:%02ld\n", tms, h, m, s); + } + sum = 0; + cur_d = ((dt + delta) / (24 * 3600)); + } + sum += (long)len; + } + if (cur_d > 0) { + tmt = localtime((time_t *)&dt); + strftime(tms, 1024, "%Y-%m-%d", tmt); + h = sum / 3600; + m = (sum - (h * 3600)) / 60; + s = (sum - (h * 3600) - (m * 60)); + printf(" %s | %02ld:%02ld:%02ld\n", tms, h, m, s); + } + +#if 0 + t = time(NULL); + printf("CTIME: %d\n", (int)t); + + memset(&tm, 0, sizeof(struct tm)); + strptime(argv[1], "%Y-%m-%d %H:%M:%S %Z", &tm); + t = mktime(&tm); + printf(" TIME: %d\n", (int)t); + + tmout = gmtime(&t); + t = mktime(tmout); + printf("GTIME: %d\n", (int)t); + + tmout = localtime(&t); + t = mktime(tmout); + printf("LTIME: %d\n", (int)t); +#endif + + exit(EXIT_SUCCESS); + +} diff --git a/howmuchuseit.c b/howmuchuseit.c new file mode 100644 index 0000000..910793d --- /dev/null +++ b/howmuchuseit.c @@ -0,0 +1,200 @@ +/* + * howmuchuseit.c + * + * Copyright (C) 2011 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. You should have received a + * copy of the GNU General Public License along with this program; if + * not, write to the Free Software Foundation, Inc, 59 Temple Place - + * Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "howmuchuseit.h" + +struct context { + FILE *fout; + time_t start; + int is_verbose; + int is_working; +}; + +struct context *g_ctx = NULL; + +void sig_handler(int sig); +void now_not_working(struct context *ctx); + +void now_working(struct context *ctx) +{ + if (ctx->is_working == 0) { + ctx->start = time(NULL); + if (ctx->is_verbose) { + printf("now is working\n"); + } + ctx->is_working = 1; + } +} + +void now_not_working(struct context *ctx) +{ + time_t curtime; + + if (ctx->is_working == 1) { + curtime = time(NULL); + fprintf(ctx->fout, "%ld:%ld\n", (long int)curtime, (long int)(curtime - ctx->start)); + fflush(ctx->fout); + if (ctx->is_verbose) { + printf("now not is working\n"); + } + ctx->is_working = 0; + } +} + + +void sig_handler(int sig) +{ + if (g_ctx) { + time_t curtime; + + now_not_working(g_ctx); + curtime = time(NULL); + fprintf(g_ctx->fout, "# end at %ld\n", (long int)curtime); + fflush(g_ctx->fout); + } + + exit (0); +} + +int main(int argc, char *argv[]) +{ + time_t curtime; + struct context ctx; + int inactive_time = 10; + int argd, fd[MAX_FD], fd_n; + char fd_name[MAX_FD][PATH_MAX+1]; + int is_daemon = 0; + int is_verbose = 0; + char fd_max = -1, fd_path[PATH_MAX+1], bf[1024]; + int i; + + fd_set rfds; + struct timeval tv; + int retval; + + ctx.is_working = 0; + + for (argd = 1 ; argd < argc ; argd++) { + if (strcmp(argv[argd], "-i") == 0) { + if ((argd+1) < argc) { + inactive_time = atoi(argv[argd+1]); + argd++; + } + } + else if (strcmp(argv[argd], "-d") == 0) { + is_daemon = 1; + } + else if (strcmp(argv[argd], "-v") == 0) { + is_verbose = 1; + } + else { + break; + } + } + + if (is_daemon) { + is_verbose = 0; + } + + if (is_daemon) { + if (daemon(0, 0) != 0) { + exit(3); + } + } + + for (fd_n = 0; argd < argc ; argd++, fd_n++) { + sprintf(fd_path, "%s/%s", EVENT_PATH, argv[argd]); + strcpy(fd_name[fd_n], argv[argd]); + if ((fd[fd_n] = open(fd_path, O_RDONLY)) == -1) { + exit(1); + } + if (is_verbose) { + printf("[%s] opened for read\n", fd_path); + } + if (fd_max < fd[fd_n]) { + fd_max = fd[fd_n]; + } + } + + if ((ctx.fout = fopen(OUTPUT_FILE, "a")) == NULL) { + exit(2); + } + + curtime = time(NULL); + fprintf(ctx.fout, "# start at %ld\n", (long int)curtime); + fflush(ctx.fout); + + ctx.is_verbose = is_verbose; + g_ctx = &ctx; + + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + + if (is_verbose) { + printf("[%s] opened for append\n", OUTPUT_FILE); + } + + while (1) { + FD_ZERO(&rfds); + for (i = 0 ; i < fd_n ; i++) { + FD_SET(fd[i], &rfds); + } + tv.tv_sec = inactive_time; + tv.tv_usec = 0; + + retval = select(fd_max+1, &rfds, NULL, NULL, &tv); + /* Don't rely on the value of tv now! */ + + if (retval > 0) { + for (i = 0 ; i < fd_n ; i++) { + if(FD_ISSET(fd[i], &rfds)) { + if (is_verbose) { + printf("data from [%s]\n", fd_name[i]); + } + read(fd[i], bf, 1024); + } + } + now_working(&ctx); + } + else if (retval == 0) { + now_not_working(&ctx); + + + } + if (is_verbose) { + fflush(stdout); + } + } + + + exit (0); + return (0); +} diff --git a/howmuchuseit.conf b/howmuchuseit.conf new file mode 100644 index 0000000..81427db --- /dev/null +++ b/howmuchuseit.conf @@ -0,0 +1,18 @@ +# ssh - OpenBSD Secure Shell server +# +# The OpenSSH server provides secure shell access to the system. + +description "OpenSSH server" + +start on filesystem +stop on runlevel [!2345] + +respawn +respawn limit 10 5 +umask 022 +# replaces SSHD_OOM_ADJUST in /etc/default/ssh +# oom never + +# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the +# 'exec' line here instead +exec /usr/local/bin/howmuchuseit -i 60 mice event4 diff --git a/howmuchuseit.h b/howmuchuseit.h new file mode 100644 index 0000000..2a64bd2 --- /dev/null +++ b/howmuchuseit.h @@ -0,0 +1,27 @@ +/* + * howmuchuseit.h + * + * Copyright (C) 2011 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. You should have received a + * copy of the GNU General Public License along with this program; if + * not, write to the Free Software Foundation, Inc, 59 Temple Place - + * Suite 330, Boston, MA 02111-1307, USA. + * + */ + + +#include +#include + +#define EVENT_PATH "/dev/input" +#define OUTPUT_FILE "/var/log/howmuchuseit.log" +#define MAX_FD 16 diff --git a/makefile b/makefile new file mode 100644 index 0000000..c6b0bd9 --- /dev/null +++ b/makefile @@ -0,0 +1,27 @@ +# +# Project howmuchuseit +# + +VER=0.0.3 +EXE=howmuchuseit howmucheck +OBJ=howmuchuseit.o +PKGNAME=howmuchuseit +TARGET=$(EXE) + +CC?=gcc + +ALL: $(TARGET) + +howmuchuseit: howmuchuseit.c howmuchuseit.h + $(CC) -Wall -o $@ $< + +howmucheck: howmucheck.c howmuchuseit.h + $(CC) -Wall -o $@ $< + +clean: + rm -f $(EXE) $(OBJ) *~ $(TARGET) + +pkg: + tar -zcv --transform="s,^,$(PKGNAME)-$(VER)/," -f ../$(PKGNAME)_$(VER).tgz * + +.PHONY: clean pkg ALL