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