uptime.c
author Mikael Berthe <mikael@lilotux.net>
Thu, 11 Nov 2010 22:42:45 +0100
changeset 7 6524d7d3061b
parent 6 55944382d88b
child 8 af9ebb57baf6
permissions -rw-r--r--
Remove option uptime_hz Shouldn't be necessary anymore.
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
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
time_t starttime = 0;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
void do_uptime (char *arg)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
{
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
	GString *line = g_string_new (NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
	guint seconds = time (NULL) - starttime;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
	guint minutes = seconds / 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
	guint hours   = minutes / 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
	guint days    = hours   / 24;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
	seconds %= 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
	minutes %= 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
	hours   %= 24;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
	if (days)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
		g_string_append_printf (line, " %u days", days);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
	if (hours)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
		g_string_append_printf (line, " %u hours", hours);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
	if (minutes)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
		g_string_append_printf (line, " %u minutes", minutes);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
	if (seconds)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
		g_string_append_printf (line, " %u seconds", seconds);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
	scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds");
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
	g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    83
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
gchar *g_module_check_init (GModule *module)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
{
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
	if (settings_opt_get_int ("uptime_use_proc")) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
		long long unsigned int mstime = 0;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
		long long unsigned int kbtime = 0;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
		GError     *error   = NULL;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
		GString    *line;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
		GIOChannel *channel = g_io_channel_new_file ("/proc/self/stat", "r", &error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
		if (!channel)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
			return "Cannot open own stats for reading";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
		g_io_channel_set_close_on_unref (channel, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
		line = g_string_new (NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
		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
    99
			if (error) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
				scr_log_print (LPRINT_DEBUG, "uptime: Own stats reading error: %s.", error -> message);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
				g_error_free (error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
			}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
			g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
			g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
			return "Error reading own stats";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
		g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
		gchar *p = strrchr (line -> str, ')'); // end of command
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
		if (!p) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
			g_string_free (line, TRUE);
1
2b0115cae8b2 Various fixes to last-minute fixes...
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   111
			return "Missing ) in own stats";
0
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
		while (*p && isalpha (*p)) p++; // state (%c)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
		while (*p && isdigit (*p)) p++; // ppid (this and next are %d)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
		while (*p && isdigit (*p)) p++; // pgrp
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
		while (*p && isdigit (*p)) p++; // session
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
		while (*p && isdigit (*p)) p++; // tty_nr
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
		while (*p && isdigit (*p)) p++; // tpgid
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
		while (*p && isdigit (*p)) p++; // flags (this and all next - %u)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
		while (*p && isdigit (*p)) p++; // minflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
		while (*p && isdigit (*p)) p++; // cminflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
		while (*p && isdigit (*p)) p++; // majflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
		while (*p && isdigit (*p)) p++; // cmajflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
		while (*p && isdigit (*p)) p++; // utime
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
		while (*p && isdigit (*p)) p++; // stime
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
		while (*p && isdigit (*p)) p++; // cutime (this and next next are %d)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   141
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
		while (*p && isdigit (*p)) p++; // cstime
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
		while (*p && isdigit (*p)) p++; // priority
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
		while (*p && isdigit (*p)) p++; // nice
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
		while (*p && isdigit (*p)) p++; // num_threads
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
		while (*p && isdigit (*p)) p++; // itrealvalue
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
		char *u = p;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
		while (*u && isdigit (*u)) u++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
		*u = '\0';
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
		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
   156
			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
   157
				"%" 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
   158
				"remaining string: %s.",
55944382d88b Use sysconf(_SC_CLK_TCK) and G_GSIZE_MODIFIER
Mikael Berthe <mikael@lilotux.net>
parents: 4
diff changeset
   159
				p - line -> str, line -> len, p);
0
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
			g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
			return "Malformed own start time.";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
		channel = g_io_channel_new_file ("/proc/stat", "r", &error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
		if (!channel) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
			g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
			return "Cannot open system stats for reading";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   168
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
		g_io_channel_set_close_on_unref (channel, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
		while (TRUE) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
			GIOStatus ret = g_io_channel_read_line_string (channel, line, NULL, &error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
			if (ret != G_IO_STATUS_NORMAL) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
				if (error) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
					scr_log_print (LPRINT_DEBUG, "uptime: System stats reading error: %s.", error -> message);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
					g_error_free (error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
				}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   178
				g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
				g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
				return "Error reading system stats.";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
			}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
			if (sscanf (line -> str, "btime %llu", &kbtime))
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
				break;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
		g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   186
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   187
		g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
7
6524d7d3061b Remove option uptime_hz
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   189
		guint hz = sysconf(_SC_CLK_TCK);
0
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
		starttime = kbtime + (mstime / hz);
1
2b0115cae8b2 Various fixes to last-minute fixes...
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   191
	} else
0
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
		starttime = time (NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
	cmd_add ("uptime", "", 0, 0, do_uptime, NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   195
	return NULL;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
void g_module_unload (GModule *module)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
{
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
	cmd_del ("uptime");
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   201
}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   202
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   203
/* The End */