author | Mikael Berthe <mikael@lilotux.net> |
Sun, 14 Nov 2010 11:02:15 +0100 | |
changeset 9 | c1f0277182a3 |
parent 8 | af9ebb57baf6 |
child 10 | 36e7f4269583 |
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); |
|
9 | 102 |
gchar strstartdate[256]; |
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
103 |
|
9 | 104 |
guint seconds = time (NULL) - starttime; |
105 |
guint minutes = seconds / 60; |
|
106 |
guint hours = minutes / 60; |
|
107 |
guint days = hours / 24; |
|
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
108 |
|
0 | 109 |
seconds %= 60; |
110 |
minutes %= 60; |
|
111 |
hours %= 24; |
|
112 |
||
113 |
if (days) |
|
114 |
g_string_append_printf (line, " %u days", days); |
|
115 |
if (hours) |
|
116 |
g_string_append_printf (line, " %u hours", hours); |
|
117 |
if (minutes) |
|
118 |
g_string_append_printf (line, " %u minutes", minutes); |
|
119 |
if (seconds) |
|
120 |
g_string_append_printf (line, " %u seconds", seconds); |
|
121 |
||
9 | 122 |
// Running since: |
123 |
strftime(strstartdate, sizeof(strstartdate), |
|
124 |
"%Y-%m-%d %H:%M", localtime(&starttime)); |
|
125 |
||
126 |
scr_log_print (LPRINT_NORMAL, "Uptime: %s.\n(Running since %s)", |
|
127 |
line -> len ? line -> str : " 0 second", |
|
128 |
strstartdate); |
|
0 | 129 |
|
130 |
g_string_free (line, TRUE); |
|
131 |
} |
|
132 |
||
133 |
gchar *g_module_check_init (GModule *module) |
|
134 |
{ |
|
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
135 |
starttime = time (NULL); |
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
136 |
|
0 | 137 |
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
|
138 |
//long long unsigned int kbtime = 0; |
0 | 139 |
GError *error = NULL; |
140 |
GString *line; |
|
141 |
GIOChannel *channel = g_io_channel_new_file ("/proc/self/stat", "r", &error); |
|
142 |
||
143 |
if (!channel) |
|
144 |
return "Cannot open own stats for reading"; |
|
145 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
146 |
||
147 |
line = g_string_new (NULL); |
|
148 |
if (g_io_channel_read_line_string (channel, line, NULL, &error) != G_IO_STATUS_NORMAL) { |
|
149 |
if (error) { |
|
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
150 |
scr_log_print (LPRINT_LOGNORM, "uptime: Own stats reading error: %s.", error -> message); |
0 | 151 |
g_error_free (error); |
152 |
} |
|
153 |
g_io_channel_unref (channel); |
|
154 |
g_string_free (line, TRUE); |
|
155 |
return "Error reading own stats"; |
|
156 |
} |
|
157 |
g_io_channel_unref (channel); |
|
158 |
gchar *p = strrchr (line -> str, ')'); // end of command |
|
159 |
if (!p) { |
|
160 |
g_string_free (line, TRUE); |
|
1
2b0115cae8b2
Various fixes to last-minute fixes...
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
161 |
return "Missing ) in own stats"; |
0 | 162 |
} |
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
163 |
p++; |
0 | 164 |
while (*p && isspace (*p)) p++; |
165 |
while (*p && isalpha (*p)) p++; // state (%c) |
|
166 |
while (*p && isspace (*p)) p++; |
|
167 |
while (*p && isdigit (*p)) p++; // ppid (this and next are %d) |
|
168 |
while (*p && isspace (*p)) p++; |
|
169 |
while (*p && isdigit (*p)) p++; // pgrp |
|
170 |
while (*p && isspace (*p)) p++; |
|
171 |
while (*p && isdigit (*p)) p++; // session |
|
172 |
while (*p && isspace (*p)) p++; |
|
173 |
while (*p && isdigit (*p)) p++; // tty_nr |
|
174 |
while (*p && isspace (*p)) p++; |
|
175 |
while (*p && isdigit (*p)) p++; // tpgid |
|
176 |
while (*p && isspace (*p)) p++; |
|
177 |
while (*p && isdigit (*p)) p++; // flags (this and all next - %u) |
|
178 |
while (*p && isspace (*p)) p++; |
|
179 |
while (*p && isdigit (*p)) p++; // minflt |
|
180 |
while (*p && isspace (*p)) p++; |
|
181 |
while (*p && isdigit (*p)) p++; // cminflt |
|
182 |
while (*p && isspace (*p)) p++; |
|
183 |
while (*p && isdigit (*p)) p++; // majflt |
|
184 |
while (*p && isspace (*p)) p++; |
|
185 |
while (*p && isdigit (*p)) p++; // cmajflt |
|
186 |
while (*p && isspace (*p)) p++; |
|
187 |
while (*p && isdigit (*p)) p++; // utime |
|
188 |
while (*p && isspace (*p)) p++; |
|
189 |
while (*p && isdigit (*p)) p++; // stime |
|
190 |
while (*p && isspace (*p)) p++; |
|
191 |
while (*p && isdigit (*p)) p++; // cutime (this and next next are %d) |
|
192 |
while (*p && isspace (*p)) p++; |
|
193 |
while (*p && isdigit (*p)) p++; // cstime |
|
194 |
while (*p && isspace (*p)) p++; |
|
195 |
while (*p && isdigit (*p)) p++; // priority |
|
196 |
while (*p && isspace (*p)) p++; |
|
197 |
while (*p && isdigit (*p)) p++; // nice |
|
198 |
while (*p && isspace (*p)) p++; |
|
199 |
while (*p && isdigit (*p)) p++; // num_threads |
|
200 |
while (*p && isspace (*p)) p++; |
|
201 |
while (*p && isdigit (*p)) p++; // itrealvalue |
|
202 |
while (*p && isspace (*p)) p++; |
|
203 |
char *u = p; |
|
204 |
while (*u && isdigit (*u)) u++; |
|
205 |
*u = '\0'; |
|
206 |
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
|
207 |
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
|
208 |
"%" 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
|
209 |
"remaining string: %s.", |
55944382d88b
Use sysconf(_SC_CLK_TCK) and G_GSIZE_MODIFIER
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
210 |
p - line -> str, line -> len, p); |
0 | 211 |
g_string_free (line, TRUE); |
212 |
return "Malformed own start time."; |
|
213 |
} |
|
214 |
g_string_free (line, TRUE); |
|
215 |
||
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
216 |
hz = sysconf(_SC_CLK_TCK); |
9 | 217 |
if (hz && mstime) { |
218 |
int sysup = sys_uptime(); |
|
219 |
if (sysup) // Let's calculate starttime more precisely: |
|
220 |
starttime = time (NULL) - (sysup - mstime) / hz; |
|
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
221 |
proc_used = TRUE; |
9 | 222 |
} |
8
af9ebb57baf6
(Hopefully) Fix usage of the proc filesystem
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
223 |
} |
0 | 224 |
|
225 |
cmd_add ("uptime", "", 0, 0, do_uptime, NULL); |
|
226 |
return NULL; |
|
227 |
} |
|
228 |
||
229 |
void g_module_unload (GModule *module) |
|
230 |
{ |
|
231 |
cmd_del ("uptime"); |
|
232 |
} |
|
233 |
||
234 |
/* The End */ |