uptime.c
author Mikael Berthe <mikael@lilotux.net>
Sat, 13 Nov 2010 17:51:20 +0100
changeset 8 af9ebb57baf6
parent 7 6524d7d3061b
child 9 c1f0277182a3
permissions -rw-r--r--
(Hopefully) Fix usage of the proc filesystem


/* Copyright 2010 Myhailo Danylenko
 *
 * This file is part of mcabber-uptime
 *
 * mcabber-uptime 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, see <http://www.gnu.org/licenses/>. */

#include <glib.h>
#include <gmodule.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <mcabber/modules.h>
#include <mcabber/logprint.h>
#include <mcabber/commands.h>
#include <mcabber/settings.h>

#include "config.h"

#define DESCRIPTION ( "Shows mcabber uptime." )

module_info_t info_uptime_experimental = {
	.branch      = "experimental",
	.api         = 15,
	.version     = PROJECT_VERSION,
	.description = DESCRIPTION,
	.requires    = NULL,
	.init        = NULL,
	.uninit      = NULL,
	.next        = NULL,
};

module_info_t info_uptime = {
	.branch      = "dev",
	.api         = 13,
	.version     = PROJECT_VERSION,
	.description = DESCRIPTION,
	.requires    = NULL,
	.init        = NULL,
	.uninit      = NULL,
	.next        = &info_uptime_experimental,
};

guint hz = 0;
long long unsigned int mstime = 0;
time_t starttime = 0;
gboolean proc_used = FALSE;

static int sys_uptime (void)
{
	GString *line = g_string_new (NULL);
	GError     *error   = NULL;
	GIOStatus ret;
	int sec, ssec;
	GIOChannel *channel = g_io_channel_new_file ("/proc/uptime", "r", &error);
	if (!channel) {
		g_string_free (line, TRUE);
		scr_log_print (LPRINT_LOGNORM,
				"Cannot open system uptime for reading.");
		return 0;
	}
	g_io_channel_set_close_on_unref (channel, TRUE);

	ret = g_io_channel_read_line_string (channel, line, NULL, &error);
	if (ret != G_IO_STATUS_NORMAL) {
		if (error) {
			scr_log_print (LPRINT_LOGNORM,
					"uptime: System uptime reading error: %s.",
					error -> message);
			g_error_free (error);
		}
		g_io_channel_unref (channel);
		g_string_free (line, TRUE);
		return 0;
	}
	g_io_channel_unref (channel);
	if (sscanf (line -> str, "%d.%ds", &sec, &ssec) != 2) {
		scr_log_print (LPRINT_LOGNORM,
				"Unable to parse system uptime.");
		g_string_free (line, TRUE);
		return 0;
	}
	return (sec * hz) + (ssec * hz)/100;
}

void do_uptime (char *arg)
{
	GString *line = g_string_new (NULL);
	guint seconds;
	guint minutes;
	guint hours;
	guint days;
	time_t now = time (NULL);

	if (proc_used && settings_opt_get_int ("uptime_use_proc")) {
		int sysup = sys_uptime();
		if (!sysup) {
			g_string_free (line, TRUE);
			scr_log_print (LPRINT_NORMAL, "Uptime not available :-(");
			return;
		}
		seconds = (sysup - mstime) / hz;
	} else {
		seconds = now - starttime;
	}

	minutes = seconds / 60;
	hours   = minutes / 60;
	days    = hours   / 24;
	seconds %= 60;
	minutes %= 60;
	hours   %= 24;

	if (days)
		g_string_append_printf (line, " %u days", days);
	if (hours)
		g_string_append_printf (line, " %u hours", hours);
	if (minutes)
		g_string_append_printf (line, " %u minutes", minutes);
	if (seconds)
		g_string_append_printf (line, " %u seconds", seconds);

	scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds");

	g_string_free (line, TRUE);
}

gchar *g_module_check_init (GModule *module)
{
	starttime = time (NULL);

	if (settings_opt_get_int ("uptime_use_proc")) {
		//long long unsigned int kbtime = 0;
		GError     *error   = NULL;
		GString    *line;
		GIOChannel *channel = g_io_channel_new_file ("/proc/self/stat", "r", &error);

		if (!channel)
			return "Cannot open own stats for reading";
		g_io_channel_set_close_on_unref (channel, TRUE);

		line = g_string_new (NULL);
		if (g_io_channel_read_line_string (channel, line, NULL, &error) != G_IO_STATUS_NORMAL) {
			if (error) {
				scr_log_print (LPRINT_LOGNORM, "uptime: Own stats reading error: %s.", error -> message);
				g_error_free (error);
			}
			g_io_channel_unref (channel);
			g_string_free (line, TRUE);
			return "Error reading own stats";
		}
		g_io_channel_unref (channel);
		gchar *p = strrchr (line -> str, ')'); // end of command
		if (!p) {
			g_string_free (line, TRUE);
			return "Missing ) in own stats";
		}
		p++;
		while (*p && isspace (*p)) p++;
		while (*p && isalpha (*p)) p++; // state (%c)
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // ppid (this and next are %d)
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // pgrp
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // session
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // tty_nr
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // tpgid
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // flags (this and all next - %u)
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // minflt
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // cminflt
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // majflt
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // cmajflt
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // utime
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // stime
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // cutime (this and next next are %d)
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // cstime
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // priority
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // nice
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // num_threads
		while (*p && isspace (*p)) p++;
		while (*p && isdigit (*p)) p++; // itrealvalue
		while (*p && isspace (*p)) p++;
		char *u = p;
		while (*u && isdigit (*u)) u++;
		*u = '\0';
		if (!sscanf (p, "%llu", &mstime)) {
			scr_log_print (LPRINT_LOGNORM, "uptime: now at "
				"%" G_GSIZE_MODIFIER "u/%" G_GSIZE_MODIFIER "u, "
				"remaining string: %s.",
				p - line -> str, line -> len, p);
			g_string_free (line, TRUE);
			return "Malformed own start time.";
		}
		g_string_free (line, TRUE);

		hz = sysconf(_SC_CLK_TCK);
		if (hz && mstime)
			proc_used = TRUE;
	}

	cmd_add ("uptime", "", 0, 0, do_uptime, NULL);
	return NULL;
}

void g_module_unload (GModule *module)
{
	cmd_del ("uptime");
}

/* The End */