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