/* * Dinko Korunic 'kreator', 2002. * Uptime hack LKM * * Copyright (C) 2002 Dinko Korunic * * 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 * MERCHANTABILITY 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 * * Usage: * insmod uptime_hack uptime=seconds * * Example: * vampirella# uptime * 21:55:46 up 1:03, 3 users, load average: 0.07, 0.02, 0.00 * vampirella# insmod uptime_hack.o uptime=10000000 * uptime_hack 1.0 initialised * vampirella# uptime * 21:55:56 up 115 days, 17:46, 3 users, load average: 0.06, 0.02, 0.00 * vampirella# rmmod uptime_hack * vampirella# uptime * 21:56:04 up 1:03, 3 users, load average: 0.05, 0.02, 0.00 * * Credits: * Big thanks goes to Matija Nalis for the original idea - as well as * numerous trolls on hr.comp.os.linux for showing us their uptime. */ #include #include #include #include #define MODULE_VERSION "1.3" #define MODULE_NAME "uptime_hack" static long unsigned myuptime = 0; static long unsigned myidle = 0; static long unsigned uptime = 0; static long unsigned startjiffies = 0; MODULE_PARM(uptime, "l"); static int proc_calc_metrics(char *page, char **start, off_t off, int count, int *eof, int len) { if (len <= off + count) *eof = 1; *start = page + off; len -= off; if (len > count) len = count; if (len < 0) len = 0; return len; } static int uptime_read(char *page, char **start, off_t off, int count, int *eof, void *data) { int len; if (!uptime) myuptime = jiffies; else myuptime = uptime * HZ + jiffies - startjiffies; #if HZ != 100 len = sprintf(page,"%lu.%02lu %lu.%02lu\n", myuptime / HZ, (((myuptime % HZ) * 100) / HZ) % 100, myidle / HZ, (((myidle % HZ) * 100) / HZ) % 100); #else len = sprintf(page,"%lu.%02lu %lu.%02lu\n", myuptime / HZ, myuptime % HZ, myidle / HZ, myidle % HZ); #endif return proc_calc_metrics(page, start, off, count, eof, len); } int init_module() { startjiffies = jiffies; /* * unfortunately, no init_tasks[] for modules :( myidle = init_tasks[0]->times.tms_utime + init_tasks[0]->times.tms_stime; */ create_proc_read_entry("uptime", 0, NULL, uptime_read, NULL); printk(KERN_INFO "%s %s initialised\n", MODULE_NAME, MODULE_VERSION); return 0; } void cleanup_module() { remove_proc_entry("uptime", NULL); } MODULE_LICENSE("GPL"); MODULE_AUTHOR("Dinko Korunic"); MODULE_DESCRIPTION("procfs uptime hack");