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