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-- |
0 | 1 |
|
2 |
/* Copyright 2010 Myhailo Danylenko |
|
3 |
* |
|
4 |
* This file is part of mcabber-uptime |
|
5 |
* |
|
6 |
* mcabber-uptime is free software: you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation, either version 2 of the License, or |
|
9 |
* (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
18 |
||
19 |
#include <glib.h> |
|
20 |
#include <gmodule.h> |
|
21 |
#include <time.h> |
|
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 | 25 |
|
26 |
#include <mcabber/modules.h> |
|
27 |
#include <mcabber/logprint.h> |
|
28 |
#include <mcabber/commands.h> |
|
29 |
#include <mcabber/settings.h> |
|
30 |
||
31 |
#include "config.h" |
|
32 |
||
7 | 33 |
#define DESCRIPTION ( "Shows mcabber uptime." ) |
0 | 34 |
|
35 |
module_info_t info_uptime_experimental = { |
|
36 |
.branch = "experimental", |
|
4 | 37 |
.api = 15, |
0 | 38 |
.version = PROJECT_VERSION, |
39 |
.description = DESCRIPTION, |
|
40 |
.requires = NULL, |
|
41 |
.init = NULL, |
|
42 |
.uninit = NULL, |
|
43 |
.next = NULL, |
|
44 |
}; |
|
45 |
||
46 |
module_info_t info_uptime = { |
|
47 |
.branch = "dev", |
|
4 | 48 |
.api = 13, |
0 | 49 |
.version = PROJECT_VERSION, |
50 |
.description = DESCRIPTION, |
|
51 |
.requires = NULL, |
|
52 |
.init = NULL, |
|
53 |
.uninit = NULL, |
|
54 |
.next = &info_uptime_experimental, |
|
55 |
}; |
|
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 | 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 | 98 |
|
99 |
void do_uptime (char *arg) |
|
100 |
{ |
|
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 | 123 |
seconds %= 60; |
124 |
minutes %= 60; |
|
125 |
hours %= 24; |
|
126 |
||
127 |
if (days) |
|
128 |
g_string_append_printf (line, " %u days", days); |
|
129 |
if (hours) |
|
130 |
g_string_append_printf (line, " %u hours", hours); |
|
131 |
if (minutes) |
|
132 |
g_string_append_printf (line, " %u minutes", minutes); |
|
133 |
if (seconds) |
|
134 |
g_string_append_printf (line, " %u seconds", seconds); |
|
135 |
||
136 |
scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds"); |
|
137 |
||
138 |
g_string_free (line, TRUE); |
|
139 |
} |
|
140 |
||
141 |
gchar *g_module_check_init (GModule *module) |
|
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 | 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 | 147 |
GError *error = NULL; |
148 |
GString *line; |
|
149 |
GIOChannel *channel = g_io_channel_new_file ("/proc/self/stat", "r", &error); |
|
150 |
||
151 |
if (!channel) |
|
152 |
return "Cannot open own stats for reading"; |
|
153 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
154 |
||
155 |
line = g_string_new (NULL); |
|
156 |
if (g_io_channel_read_line_string (channel, line, NULL, &error) != G_IO_STATUS_NORMAL) { |
|
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 | 159 |
g_error_free (error); |
160 |
} |
|
161 |
g_io_channel_unref (channel); |
|
162 |
g_string_free (line, TRUE); |
|
163 |
return "Error reading own stats"; |
|
164 |
} |
|
165 |
g_io_channel_unref (channel); |
|
166 |
gchar *p = strrchr (line -> str, ')'); // end of command |
|
167 |
if (!p) { |
|
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 | 170 |
} |
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
171 |
p++; |
0 | 172 |
while (*p && isspace (*p)) p++; |
173 |
while (*p && isalpha (*p)) p++; // state (%c) |
|
174 |
while (*p && isspace (*p)) p++; |
|
175 |
while (*p && isdigit (*p)) p++; // ppid (this and next are %d) |
|
176 |
while (*p && isspace (*p)) p++; |
|
177 |
while (*p && isdigit (*p)) p++; // pgrp |
|
178 |
while (*p && isspace (*p)) p++; |
|
179 |
while (*p && isdigit (*p)) p++; // session |
|
180 |
while (*p && isspace (*p)) p++; |
|
181 |
while (*p && isdigit (*p)) p++; // tty_nr |
|
182 |
while (*p && isspace (*p)) p++; |
|
183 |
while (*p && isdigit (*p)) p++; // tpgid |
|
184 |
while (*p && isspace (*p)) p++; |
|
185 |
while (*p && isdigit (*p)) p++; // flags (this and all next - %u) |
|
186 |
while (*p && isspace (*p)) p++; |
|
187 |
while (*p && isdigit (*p)) p++; // minflt |
|
188 |
while (*p && isspace (*p)) p++; |
|
189 |
while (*p && isdigit (*p)) p++; // cminflt |
|
190 |
while (*p && isspace (*p)) p++; |
|
191 |
while (*p && isdigit (*p)) p++; // majflt |
|
192 |
while (*p && isspace (*p)) p++; |
|
193 |
while (*p && isdigit (*p)) p++; // cmajflt |
|
194 |
while (*p && isspace (*p)) p++; |
|
195 |
while (*p && isdigit (*p)) p++; // utime |
|
196 |
while (*p && isspace (*p)) p++; |
|
197 |
while (*p && isdigit (*p)) p++; // stime |
|
198 |
while (*p && isspace (*p)) p++; |
|
199 |
while (*p && isdigit (*p)) p++; // cutime (this and next next are %d) |
|
200 |
while (*p && isspace (*p)) p++; |
|
201 |
while (*p && isdigit (*p)) p++; // cstime |
|
202 |
while (*p && isspace (*p)) p++; |
|
203 |
while (*p && isdigit (*p)) p++; // priority |
|
204 |
while (*p && isspace (*p)) p++; |
|
205 |
while (*p && isdigit (*p)) p++; // nice |
|
206 |
while (*p && isspace (*p)) p++; |
|
207 |
while (*p && isdigit (*p)) p++; // num_threads |
|
208 |
while (*p && isspace (*p)) p++; |
|
209 |
while (*p && isdigit (*p)) p++; // itrealvalue |
|
210 |
while (*p && isspace (*p)) p++; |
|
211 |
char *u = p; |
|
212 |
while (*u && isdigit (*u)) u++; |
|
213 |
*u = '\0'; |
|
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 | 219 |
g_string_free (line, TRUE); |
220 |
return "Malformed own start time."; |
|
221 |
} |
|
222 |
g_string_free (line, TRUE); |
|
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 | 228 |
|
229 |
cmd_add ("uptime", "", 0, 0, do_uptime, NULL); |
|
230 |
return NULL; |
|
231 |
} |
|
232 |
||
233 |
void g_module_unload (GModule *module) |
|
234 |
{ |
|
235 |
cmd_del ("uptime"); |
|
236 |
} |
|
237 |
||
238 |
/* The End */ |