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