~akhil011/ubuntu/wily/monit/crypto_fix

« back to all changes in this revision

Viewing changes to src/process/sysdep_HURD.c

  • Committer: Package Import Robot
  • Author(s): Sergey B Kirpichev
  • Date: 2012-12-13 17:01:58 UTC
  • Revision ID: package-import@ubuntu.com-20121213170158-bap846tksqh3mhfw
Tags: 1:5.5-5
* Fix FTBFS on ia64, a second attempt
* Revert back -c option in monit.init
* Implement sysdep_HURD.c
* Correct description for patch 11
* Add missing DEP3 "Forwarded" headers

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) Tildeslash Ltd. All rights reserved.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Affero General Public License version 3.
 
6
 *
 
7
 * This program is distributed in the hope that it will be useful,
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 * GNU General Public License for more details.
 
11
 *
 
12
 * You should have received a copy of the GNU Affero General Public License
 
13
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 *
 
15
 * In addition, as a special exception, the copyright holders give
 
16
 * permission to link the code of portions of this program with the
 
17
 * OpenSSL library under certain conditions as described in each
 
18
 * individual source file, and distribute linked combinations
 
19
 * including the two.
 
20
 *
 
21
 * You must obey the GNU Affero General Public License in all respects
 
22
 * for all of the code used other than OpenSSL.
 
23
 */
 
24
 
 
25
/*
 
26
 HURD note.
 
27
 
 
28
 The code below was copy-pasted from sysdep_LINUX.c
 
29
 with only hack for HZ #define.
 
30
*/
 
31
 
 
32
#include "config.h"
 
33
 
 
34
#ifdef HAVE_STDIO_H
 
35
#include <stdio.h>
 
36
#endif
 
37
 
 
38
#ifdef HAVE_ERRNO_H
 
39
#include <errno.h>
 
40
#endif
 
41
 
 
42
#ifdef HAVE_SYS_TYPES_H
 
43
#include <sys/types.h>
 
44
#endif
 
45
 
 
46
#ifdef HAVE_UNISTD_H
 
47
#include <unistd.h>
 
48
#endif
 
49
 
 
50
#ifdef HAVE_SYS_STAT_H
 
51
#include <sys/stat.h>
 
52
#endif
 
53
 
 
54
#ifdef HAVE_FCNTL_H
 
55
#include <fcntl.h>
 
56
#endif
 
57
 
 
58
#ifdef HAVE_STDLIB_H
 
59
#include <stdlib.h>
 
60
#endif
 
61
 
 
62
#ifdef HAVE_UNISTD_H
 
63
#include <unistd.h>
 
64
#endif
 
65
 
 
66
#ifdef TIME_WITH_SYS_TIME
 
67
#include <time.h>
 
68
 
 
69
#ifdef HAVE_SYS_TIME_H
 
70
#include <sys/time.h>
 
71
#endif
 
72
#else
 
73
#include <time.h>
 
74
#endif
 
75
 
 
76
#ifdef HAVE_STRING_H
 
77
#include <string.h>
 
78
#endif
 
79
 
 
80
#ifdef HAVE_ASM_PARAM_H
 
81
#include <asm/param.h>
 
82
#endif
 
83
 
 
84
#ifdef HAVE_GLOB_H
 
85
#include <glob.h>
 
86
#endif
 
87
 
 
88
/*
 
89
 
 
90
 On GNU/Hurd procps usually launched with -c (linux-compatibility switch):
 
91
 http://lists.debian.org/debian-hurd/2012/10/msg00070.html
 
92
 so, sysconf(_SC_CLK_TCK_) is useless here right now...
 
93
 
 
94
 */
 
95
 
 
96
#ifndef HZ
 
97
# ifndef HURD
 
98
#  define HZ sysconf(_SC_CLK_TCK)
 
99
# else
 
100
#  define HZ 100
 
101
# endif
 
102
#endif
 
103
 
 
104
#include "monit.h"
 
105
#include "process.h"
 
106
#include "process_sysdep.h"
 
107
 
 
108
 
 
109
/**
 
110
 *  System dependent resource gathering code for Linux.
 
111
 *
 
112
 *  @file
 
113
 */
 
114
 
 
115
 
 
116
/* ----------------------------------------------------------------- Private */
 
117
 
 
118
 
 
119
#define MEMTOTAL  "MemTotal:"
 
120
#define MEMFREE   "MemFree:"
 
121
#define MEMBUF    "Buffers:"
 
122
#define MEMCACHE  "Cached:"
 
123
#define SWAPTOTAL "SwapTotal:"
 
124
#define SWAPFREE  "SwapFree:"
 
125
 
 
126
#define NSEC_PER_SEC    1000000000L
 
127
 
 
128
static unsigned long long old_cpu_user     = 0;
 
129
static unsigned long long old_cpu_syst     = 0;
 
130
static unsigned long long old_cpu_wait     = 0;
 
131
static unsigned long long old_cpu_total    = 0;
 
132
static int                page_shift_to_kb = 0;
 
133
 
 
134
 
 
135
/**
 
136
 * Get system start time
 
137
 * @return seconds since unix epoch
 
138
 */
 
139
static time_t get_starttime() {
 
140
  char   buf[1024];
 
141
  double up = 0;
 
142
 
 
143
  if (! read_proc_file(buf, 1024, "uptime", -1, NULL)) {
 
144
    LogError("system statistic error -- cannot get system uptime\n");
 
145
    return 0;
 
146
  }
 
147
 
 
148
  if (sscanf(buf, "%lf", &up) != 1) {
 
149
    LogError("system statistic error -- invalid uptime\n");
 
150
    return 0;
 
151
  }
 
152
 
 
153
  return time(NULL) - (time_t)up;
 
154
}
 
155
 
 
156
 
 
157
/* ------------------------------------------------------------------ Public */
 
158
 
 
159
 
 
160
int init_process_info_sysdep(void) {
 
161
  char *ptr;
 
162
  char  buf[1024];
 
163
  long  page_size;
 
164
  int   page_shift;
 
165
 
 
166
  if (! read_proc_file(buf, sizeof(buf), "meminfo", -1, NULL))
 
167
    return FALSE;
 
168
  if (! (ptr = strstr(buf, MEMTOTAL))) {
 
169
    DEBUG("system statistic error -- cannot get real memory amount\n");
 
170
    return FALSE;
 
171
  }
 
172
  if (sscanf(ptr+strlen(MEMTOTAL), "%ld", &systeminfo.mem_kbyte_max) != 1) {
 
173
    DEBUG("system statistic error -- cannot get real memory amount\n");
 
174
    return FALSE;
 
175
  }
 
176
 
 
177
  if ((systeminfo.cpus = sysconf(_SC_NPROCESSORS_CONF)) < 0) {
 
178
    DEBUG("system statistic error -- cannot get cpu count: %s\n", STRERROR);
 
179
    return FALSE;
 
180
  } else if (systeminfo.cpus == 0) {
 
181
    DEBUG("system reports cpu count 0, setting dummy cpu count 1\n");
 
182
    systeminfo.cpus = 1;
 
183
  }
 
184
 
 
185
  if ((page_size = sysconf(_SC_PAGESIZE)) <= 0) {
 
186
    DEBUG("system statistic error -- cannot get page size: %s\n", STRERROR);
 
187
    return FALSE;
 
188
  }
 
189
 
 
190
  for (page_shift = 0; page_size != 1; page_size >>= 1, page_shift++);
 
191
  page_shift_to_kb = page_shift - 10;
 
192
 
 
193
  return TRUE;
 
194
}
 
195
 
 
196
 
 
197
/**
 
198
 * Read all processes of the proc files system to initialize
 
199
 * the process tree (sysdep version... but should work for
 
200
 * all procfs based unices)
 
201
 * @param reference  reference of ProcessTree
 
202
 * @return treesize>0 if succeeded otherwise =0.
 
203
 */
 
204
int initprocesstree_sysdep(ProcessTree_T ** reference) {
 
205
  int                 i = 0, j;
 
206
  int                 rv, bytes = 0;
 
207
  int                 treesize = 0;
 
208
  int                 stat_ppid = 0;
 
209
  char               *tmp = NULL;
 
210
  char                procname[STRLEN];
 
211
  char                buf[1024];
 
212
  char                stat_item_state;
 
213
  long                stat_item_cutime = 0;
 
214
  long                stat_item_cstime = 0;
 
215
  long                stat_item_rss = 0;
 
216
  glob_t              globbuf;
 
217
  unsigned long       stat_item_utime = 0;
 
218
  unsigned long       stat_item_stime = 0;
 
219
  unsigned long long  stat_item_starttime = 0ULL;
 
220
  ProcessTree_T      *pt = NULL;
 
221
 
 
222
  ASSERT(reference);
 
223
 
 
224
  /* Find all processes in the /proc directory */
 
225
  if ((rv = glob("/proc/[0-9]*", GLOB_ONLYDIR, NULL, &globbuf))) {
 
226
    LogError("system statistic error -- glob failed: %d (%s)\n", rv, STRERROR);
 
227
    return FALSE;
 
228
  }
 
229
 
 
230
  treesize = globbuf.gl_pathc;
 
231
 
 
232
  pt = CALLOC(sizeof(ProcessTree_T), treesize);
 
233
 
 
234
  /* Insert data from /proc directory */
 
235
  for (i = 0; i < treesize; i++) {
 
236
 
 
237
    pt[i].pid = atoi(globbuf.gl_pathv[i] + strlen("/proc/"));
 
238
 
 
239
    if (!read_proc_file(buf, sizeof(buf), "stat", pt[i].pid, NULL)) {
 
240
      DEBUG("system statistic error -- cannot read /proc/%d/stat\n", pt[i].pid);
 
241
      continue;
 
242
    }
 
243
 
 
244
    pt[i].time = get_float_time();
 
245
 
 
246
    if (!(tmp = strrchr(buf, ')'))) {
 
247
      DEBUG("system statistic error -- file /proc/%d/stat parse error\n", pt[i].pid);
 
248
      continue;
 
249
    }
 
250
    *tmp = 0;
 
251
    if (sscanf(buf, "%*d (%256s", procname) != 1) {
 
252
      DEBUG("system statistic error -- file /proc/%d/stat process name parse error\n", pt[i].pid);
 
253
      continue;
 
254
    }
 
255
 
 
256
    tmp += 2;
 
257
 
 
258
    /* This implementation is done by using fs/procfs/array.c as a basis
 
259
     * it is also worth looking into the source of the procps utils */
 
260
    if (sscanf(tmp,
 
261
         "%c %d %*d %*d %*d %*d %*u %*u"
 
262
         "%*u %*u %*u %lu %lu %ld %ld %*d %*d %*d "
 
263
         "%*u %llu %*u %ld %*u %*u %*u %*u %*u "
 
264
         "%*u %*u %*u %*u %*u %*u %*u %*u %*d %*d\n",
 
265
         &stat_item_state,
 
266
         &stat_ppid,
 
267
         &stat_item_utime,
 
268
         &stat_item_stime,
 
269
         &stat_item_cutime,
 
270
         &stat_item_cstime,
 
271
         &stat_item_starttime,
 
272
         &stat_item_rss) != 8) {
 
273
      DEBUG("system statistic error -- file /proc/%d/stat parse error\n", pt[i].pid);
 
274
      continue;
 
275
    }
 
276
 
 
277
    pt[i].ppid      = stat_ppid;
 
278
    pt[i].starttime = get_starttime() + (time_t)(stat_item_starttime / HZ);
 
279
 
 
280
    /* jiffies -> seconds = 1 / HZ
 
281
     * HZ is defined in "asm/param.h"  and it is usually 1/100s but on
 
282
     * alpha system it is 1/1024s */
 
283
    pt[i].cputime     = ((float)(stat_item_utime + stat_item_stime) * 10.0) / HZ;
 
284
    pt[i].cpu_percent = 0;
 
285
 
 
286
    /* State is Zombie -> then we are a Zombie ... clear or? (-: */
 
287
    if (stat_item_state == 'Z')
 
288
      pt[i].status_flag |= PROCESS_ZOMBIE;
 
289
 
 
290
    if (page_shift_to_kb < 0)
 
291
      pt[i].mem_kbyte = (stat_item_rss >> abs(page_shift_to_kb));
 
292
    else
 
293
      pt[i].mem_kbyte = (stat_item_rss << abs(page_shift_to_kb));
 
294
 
 
295
    if (! read_proc_file(buf, sizeof(buf), "cmdline", pt[i].pid, &bytes)) {
 
296
      DEBUG("system statistic error -- cannot read /proc/%d/cmdline\n", pt[i].pid);
 
297
      continue;
 
298
    }
 
299
    /* The cmdline file contains argv elements/strings terminated separated by '\0' => join the string: */
 
300
    for (j = 0; j < (bytes - 1); j++)
 
301
      if (buf[j] == 0)
 
302
        buf[j] = ' ';
 
303
    pt[i].cmdline = *buf ? Str_dup(buf) : Str_dup(procname);
 
304
  }
 
305
 
 
306
  *reference = pt;
 
307
  globfree(&globbuf);
 
308
 
 
309
  return treesize;
 
310
}
 
311
 
 
312
 
 
313
/**
 
314
 * This routine returns 'nelem' double precision floats containing
 
315
 * the load averages in 'loadv'; at most 3 values will be returned.
 
316
 * @param loadv destination of the load averages
 
317
 * @param nelem number of averages
 
318
 * @return: 0 if successful, -1 if failed (and all load averages are 0).
 
319
 */
 
320
int getloadavg_sysdep(double *loadv, int nelem) {
 
321
#ifdef HAVE_GETLOADAVG
 
322
        return getloadavg(loadv, nelem);
 
323
#else
 
324
        char buf[STRLEN];
 
325
        double load[3];
 
326
        if (! read_proc_file(buf, sizeof(buf), "loadavg", -1, NULL))
 
327
                return -1;
 
328
        if (sscanf(buf, "%lf %lf %lf", &load[0], &load[1], &load[2]) != 3) {
 
329
                DEBUG("system statistic error -- cannot get load average\n");
 
330
                return -1;
 
331
        }
 
332
        for (int i = 0; i < nelem; i++)
 
333
                loadv[i] = load[i];
 
334
        return 0;
 
335
#endif
 
336
}
 
337
 
 
338
 
 
339
/**
 
340
 * This routine returns kbyte of real memory in use.
 
341
 * @return: TRUE if successful, FALSE if failed
 
342
 */
 
343
int used_system_memory_sysdep(SystemInfo_T *si) {
 
344
  char          *ptr;
 
345
  char           buf[1024];
 
346
  unsigned long  mem_free = 0UL;
 
347
  unsigned long  buffers = 0UL;
 
348
  unsigned long  cached = 0UL;
 
349
  unsigned long  swap_total = 0UL;
 
350
  unsigned long  swap_free = 0UL;
 
351
 
 
352
  if (! read_proc_file(buf, 1024, "meminfo", -1, NULL)) {
 
353
    LogError("system statistic error -- cannot get real memory free amount\n");
 
354
    goto error;
 
355
  }
 
356
 
 
357
  /* Memory */
 
358
  if (! (ptr = strstr(buf, MEMFREE)) || sscanf(ptr + strlen(MEMFREE), "%ld", &mem_free) != 1) {
 
359
    LogError("system statistic error -- cannot get real memory free amount\n");
 
360
    goto error;
 
361
  }
 
362
  if (! (ptr = strstr(buf, MEMBUF)) || sscanf(ptr + strlen(MEMBUF), "%ld", &buffers) != 1)
 
363
    DEBUG("system statistic error -- cannot get real memory buffers amount\n");
 
364
  if (! (ptr = strstr(buf, MEMCACHE)) || sscanf(ptr + strlen(MEMCACHE), "%ld", &cached) != 1)
 
365
    DEBUG("system statistic error -- cannot get real memory cache amount\n");
 
366
  si->total_mem_kbyte = systeminfo.mem_kbyte_max - mem_free - buffers - cached;
 
367
 
 
368
  /* Swap */
 
369
  if (! (ptr = strstr(buf, SWAPTOTAL)) || sscanf(ptr + strlen(SWAPTOTAL), "%ld", &swap_total) != 1) {
 
370
    LogError("system statistic error -- cannot get swap total amount\n");
 
371
    goto error;
 
372
  }
 
373
  if (! (ptr = strstr(buf, SWAPFREE)) || sscanf(ptr + strlen(SWAPFREE), "%ld", &swap_free) != 1) {
 
374
    LogError("system statistic error -- cannot get swap free amount\n");
 
375
    goto error;
 
376
  }
 
377
  si->swap_kbyte_max   = swap_total;
 
378
  si->total_swap_kbyte = swap_total - swap_free;
 
379
 
 
380
  return TRUE;
 
381
 
 
382
  error:
 
383
  si->total_mem_kbyte = 0;
 
384
  si->swap_kbyte_max = 0;
 
385
  return FALSE;
 
386
}
 
387
 
 
388
 
 
389
/**
 
390
 * This routine returns system/user CPU time in use.
 
391
 * @return: TRUE if successful, FALSE if failed (or not available)
 
392
 */
 
393
int used_system_cpu_sysdep(SystemInfo_T *si) {
 
394
  int                rv;
 
395
  unsigned long long cpu_total;
 
396
  unsigned long long cpu_user;
 
397
  unsigned long long cpu_nice;
 
398
  unsigned long long cpu_syst;
 
399
  unsigned long long cpu_idle;
 
400
  unsigned long long cpu_wait;
 
401
  unsigned long long cpu_irq;
 
402
  unsigned long long cpu_softirq;
 
403
  char               buf[1024];
 
404
 
 
405
  if (!read_proc_file(buf, 1024, "stat", -1, NULL)) {
 
406
    LogError("system statistic error -- cannot read /proc/stat\n");
 
407
    goto error;
 
408
  }
 
409
 
 
410
  rv = sscanf(buf, "cpu %llu %llu %llu %llu %llu %llu %llu",
 
411
         &cpu_user,
 
412
         &cpu_nice,
 
413
         &cpu_syst,
 
414
         &cpu_idle,
 
415
         &cpu_wait,
 
416
         &cpu_irq,
 
417
         &cpu_softirq);
 
418
  if (rv < 4) {
 
419
    LogError("system statistic error -- cannot read cpu usage\n");
 
420
    goto error;
 
421
  } else if (rv == 4) {
 
422
    /* linux 2.4.x doesn't support these values */
 
423
    cpu_wait    = 0;
 
424
    cpu_irq     = 0;
 
425
    cpu_softirq = 0;
 
426
  }
 
427
 
 
428
  cpu_total = cpu_user + cpu_nice + cpu_syst + cpu_idle + cpu_wait + cpu_irq + cpu_softirq;
 
429
  cpu_user  = cpu_user + cpu_nice;
 
430
 
 
431
  if (old_cpu_total == 0) {
 
432
    si->total_cpu_user_percent = -10;
 
433
    si->total_cpu_syst_percent = -10;
 
434
    si->total_cpu_wait_percent = -10;
 
435
  } else {
 
436
    unsigned long long delta = cpu_total - old_cpu_total;
 
437
 
 
438
    si->total_cpu_user_percent = (int)(1000 * (double)(cpu_user - old_cpu_user) / delta);
 
439
    si->total_cpu_syst_percent = (int)(1000 * (double)(cpu_syst - old_cpu_syst) / delta);
 
440
    si->total_cpu_wait_percent = (int)(1000 * (double)(cpu_wait - old_cpu_wait) / delta);
 
441
  }
 
442
 
 
443
  old_cpu_user  = cpu_user;
 
444
  old_cpu_syst  = cpu_syst;
 
445
  old_cpu_wait  = cpu_wait;
 
446
  old_cpu_total = cpu_total;
 
447
  return TRUE;
 
448
 
 
449
  error:
 
450
  si->total_cpu_user_percent = 0;
 
451
  si->total_cpu_syst_percent = 0;
 
452
  si->total_cpu_wait_percent = 0;
 
453
  return FALSE;
 
454
}
 
455
 
 
456