uptime.c
changeset 0 3f69962cbbf4
child 1 2b0115cae8b2
equal deleted inserted replaced
-1:000000000000 0:3f69962cbbf4
       
     1 
       
     2 /* Copyright 2010 Myhailo Danylenko
       
     3  *
       
     4  * This file is part of mcabber-uptime
       
     5  *
       
     6  * mcabber-uptime is free software: you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation, either version 2 of the License, or
       
     9  * (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  * GNU General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
       
    18 
       
    19 #include <glib.h>
       
    20 #include <gmodule.h>
       
    21 #include <time.h>
       
    22 #include <stdio.h>
       
    23 
       
    24 #include <mcabber/modules.h>
       
    25 #include <mcabber/logprint.h>
       
    26 #include <mcabber/commands.h>
       
    27 #include <mcabber/settings.h>
       
    28 
       
    29 #include "config.h"
       
    30 
       
    31 #define DESCRIPTION ( "Shows mcabber uptime\nRecognizes option uptime_hz (at load time)." )
       
    32 
       
    33 module_info_t info_uptime_experimental = {
       
    34 	.branch      = "experimental",
       
    35 	.api         = 12,
       
    36 	.version     = PROJECT_VERSION,
       
    37 	.description = DESCRIPTION,
       
    38 	.requires    = NULL,
       
    39 	.init        = NULL,
       
    40 	.uninit      = NULL,
       
    41 	.next        = NULL,
       
    42 };
       
    43 
       
    44 module_info_t info_uptime = {
       
    45 	.branch      = "dev",
       
    46 	.api         = 11,
       
    47 	.version     = PROJECT_VERSION,
       
    48 	.description = DESCRIPTION,
       
    49 	.requires    = NULL,
       
    50 	.init        = NULL,
       
    51 	.uninit      = NULL,
       
    52 	.next        = &info_uptime_experimental,
       
    53 };
       
    54 
       
    55 time_t starttime = 0;
       
    56 
       
    57 void do_uptime (char *arg)
       
    58 {
       
    59 	GString *line = g_string_new (NULL);
       
    60 	guint seconds = time (NULL) - starttime;
       
    61 	guint minutes = seconds / 60;
       
    62 	guint hours   = minutes / 60;
       
    63 	guint days    = hours   / 24;
       
    64 	seconds %= 60;
       
    65 	minutes %= 60;
       
    66 	hours   %= 24;
       
    67 
       
    68 	g_string_truncate (line, 0);
       
    69 	if (days)
       
    70 		g_string_append_printf (line, " %u days", days);
       
    71 	if (hours)
       
    72 		g_string_append_printf (line, " %u hours", hours);
       
    73 	if (minutes)
       
    74 		g_string_append_printf (line, " %u minutes", minutes);
       
    75 	if (seconds)
       
    76 		g_string_append_printf (line, " %u seconds", seconds);
       
    77 
       
    78 	scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds");
       
    79 
       
    80 	g_string_free (line, TRUE);
       
    81 }
       
    82 
       
    83 gchar *g_module_check_init (GModule *module)
       
    84 {
       
    85 	if (settings_opt_get_int ("uptime_use_proc")) {
       
    86 		long long unsigned int mstime = 0;
       
    87 		long long unsigned int kbtime = 0;
       
    88 		GError     *error   = NULL;
       
    89 		GString    *line;
       
    90 		GIOChannel *channel = g_io_channel_new_file ("/proc/self/stat", "r", &error);
       
    91 
       
    92 		if (!channel)
       
    93 			return "Cannot open own stats for reading";
       
    94 		g_io_channel_set_close_on_unref (channel, TRUE);
       
    95 
       
    96 		line = g_string_new (NULL);
       
    97 		if (g_io_channel_read_line_string (channel, line, NULL, &error) != G_IO_STATUS_NORMAL) {
       
    98 			if (error) {
       
    99 				scr_log_print (LPRINT_DEBUG, "uptime: Own stats reading error: %s.", error -> message);
       
   100 				g_error_free (error);
       
   101 			}
       
   102 			g_io_channel_unref (channel);
       
   103 			g_string_free (line, TRUE);
       
   104 			return "Error reading own stats";
       
   105 		}
       
   106 		g_io_channel_unref (channel);
       
   107 		gchar *p = strrchr (line -> str, ')'); // end of command
       
   108 		if (!p) {
       
   109 			g_string_free (line, TRUE);
       
   110 			return "Missing ) in own stats"
       
   111 		}
       
   112 		while (*p && isspace (*p)) p++;
       
   113 		while (*p && isalpha (*p)) p++; // state (%c)
       
   114 		while (*p && isspace (*p)) p++;
       
   115 		while (*p && isdigit (*p)) p++; // ppid (this and next are %d)
       
   116 		while (*p && isspace (*p)) p++;
       
   117 		while (*p && isdigit (*p)) p++; // pgrp
       
   118 		while (*p && isspace (*p)) p++;
       
   119 		while (*p && isdigit (*p)) p++; // session
       
   120 		while (*p && isspace (*p)) p++;
       
   121 		while (*p && isdigit (*p)) p++; // tty_nr
       
   122 		while (*p && isspace (*p)) p++;
       
   123 		while (*p && isdigit (*p)) p++; // tpgid
       
   124 		while (*p && isspace (*p)) p++;
       
   125 		while (*p && isdigit (*p)) p++; // flags (this and all next - %u)
       
   126 		while (*p && isspace (*p)) p++;
       
   127 		while (*p && isdigit (*p)) p++; // minflt
       
   128 		while (*p && isspace (*p)) p++;
       
   129 		while (*p && isdigit (*p)) p++; // cminflt
       
   130 		while (*p && isspace (*p)) p++;
       
   131 		while (*p && isdigit (*p)) p++; // majflt
       
   132 		while (*p && isspace (*p)) p++;
       
   133 		while (*p && isdigit (*p)) p++; // cmajflt
       
   134 		while (*p && isspace (*p)) p++;
       
   135 		while (*p && isdigit (*p)) p++; // utime
       
   136 		while (*p && isspace (*p)) p++;
       
   137 		while (*p && isdigit (*p)) p++; // stime
       
   138 		while (*p && isspace (*p)) p++;
       
   139 		while (*p && isdigit (*p)) p++; // cutime (this and next next are %d)
       
   140 		while (*p && isspace (*p)) p++;
       
   141 		while (*p && isdigit (*p)) p++; // cstime
       
   142 		while (*p && isspace (*p)) p++;
       
   143 		while (*p && isdigit (*p)) p++; // priority
       
   144 		while (*p && isspace (*p)) p++;
       
   145 		while (*p && isdigit (*p)) p++; // nice
       
   146 		while (*p && isspace (*p)) p++;
       
   147 		while (*p && isdigit (*p)) p++; // num_threads
       
   148 		while (*p && isspace (*p)) p++;
       
   149 		while (*p && isdigit (*p)) p++; // itrealvalue
       
   150 		while (*p && isspace (*p)) p++;
       
   151 		char *u = p;
       
   152 		while (*u && isdigit (*u)) u++;
       
   153 		*u = '\0';
       
   154 		if (!sscanf (p, "%llu", &mstime)) {
       
   155 			scr_log_print (LPRINT_LOGNORM, "uptime: now at %u/%u, remaining string: %s.", p - line -> str, line -> len, p);
       
   156 			g_string_free (line, TRUE);
       
   157 			return "Malformed own start time.";
       
   158 		}
       
   159 
       
   160 		channel = g_io_channel_new_file ("/proc/stat", "r", &error);
       
   161 		if (!channel) {
       
   162 			g_string_free (line, TRUE);
       
   163 			return "Cannot open system stats for reading";
       
   164 		}
       
   165 		g_io_channel_set_close_on_unref (channel, TRUE);
       
   166 
       
   167 		while (TRUE) {
       
   168 			GIOStatus ret = g_io_channel_read_line_string (channel, line, NULL, &error);
       
   169 			if (ret != G_IO_STATUS_NORMAL) {
       
   170 				if (error) {
       
   171 					scr_log_print (LPRINT_DEBUG, "uptime: System stats reading error: %s.", error -> message);
       
   172 					g_error_free (error);
       
   173 				}
       
   174 				g_io_channel_unref (channel);
       
   175 				g_string_free (line, TRUE);
       
   176 				return "Error reading system stats.";
       
   177 			}
       
   178 			if (sscanf (line -> str, "btime %llu", &kbtime))
       
   179 				break;
       
   180 		}
       
   181 		g_io_channel_unref (channel);
       
   182 
       
   183 		g_string_free (line, TRUE);
       
   184 
       
   185 		guint hz = settings_opt_get_int ("uptime_hz");
       
   186 		if (!hz)
       
   187 			hz = 250;
       
   188 		starttime = kbtime + (mstime / hz);
       
   189 	} else {
       
   190 		starttime = time (NULL);
       
   191 
       
   192 	cmd_add ("uptime", "", 0, 0, do_uptime, NULL);
       
   193 	return NULL;
       
   194 }
       
   195 
       
   196 void g_module_unload (GModule *module)
       
   197 {
       
   198 	cmd_del ("uptime");
       
   199 }
       
   200 
       
   201 /* The End */