uptime.c
author Myhailo Danylenko <isbear@ukrpost.net>
Tue, 03 May 2011 18:45:53 +0300
changeset 12 b88ea7f650f1
parent 11 431de0cc8126
child 14 3f6d549befa8
permissions -rw-r--r--
Add AVV description


/* Copyright 2010 Myhailo Danylenko
 * Copyright 2010 Mikael Berthe
 *
 * 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         = 34,
	.version     = PROJECT_VERSION,
	.description = DESCRIPTION,
	.requires    = NULL,
	.init        = NULL,
	.uninit      = NULL,
	.next        = NULL,
};

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

static gpointer uptime_cmid = NULL;

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);
	gchar strstartdate[256];

	guint seconds = time (NULL) - starttime;
	guint minutes = seconds / 60;
	guint hours   = minutes / 60;
	guint days    = hours   / 24;

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

	if (days)
		g_string_append_printf (line, " %u day%c", days,
				days > 1 ? 's' : ' ');
	if (hours)
		g_string_append_printf (line, " %u hour%c", hours,
				hours > 1 ? 's' : ' ');
	if (minutes)
		g_string_append_printf (line, " %u minute%c", minutes,
				minutes > 1 ? 's' : ' ');
	if (seconds)
		g_string_append_printf (line, " %u second%s", seconds,
				seconds > 1 ? "s." : ".");

	// Running since:
	strftime(strstartdate, sizeof(strstartdate),
			"%Y-%m-%d %H:%M", localtime(&starttime));

	scr_log_print (LPRINT_NORMAL, "Uptime: %s\n(Running since %s)",
			line -> len ? line -> str : " 0 second.",
			strstartdate);

	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) {
			int sysup = sys_uptime();
			if (sysup) // Let's calculate starttime more precisely:
				starttime = time (NULL) - (sysup - mstime) / hz;
			proc_used = TRUE;
		}
	}

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

void g_module_unload (GModule *module)
{
	if (uptime_cmid)
		cmd_del (uptime_cmid);
}

/* The End */