get_rload.c revision 83f4f7f0
183f4f7f0Smrg#ifdef HAVE_CONFIG_H
283f4f7f0Smrg# include "config.h"
383f4f7f0Smrg#endif
4fd0c672fSmrg
5fd0c672fSmrg#include <stdio.h>
6fd0c672fSmrg#include <X11/Intrinsic.h>
7fd0c672fSmrg#include <fcntl.h>
8fd0c672fSmrg#include <stdlib.h>
9fd0c672fSmrg#include <unistd.h>
10fd0c672fSmrg#include "xload.h"
11fd0c672fSmrg
12fd0c672fSmrg/* Not all OS supports get_rload
13fd0c672fSmrg   steal the STUB idea from get_load
14fd0c672fSmrg */
1583f4f7f0Smrg#ifndef HAVE_PROTOCOLS_RWHOD_H
16fd0c672fSmrg#define RLOADSTUB
17fd0c672fSmrg#endif
18fd0c672fSmrg
19fd0c672fSmrg#ifdef RLOADSTUB
2048e69166Smrgvoid GetRLoadPoint(
2148e69166Smrg    Widget	w,		/* unused */
2248e69166Smrg    XtPointer	closure,	/* unused */
2348e69166Smrg    XtPointer	call_data)	/* pointer to (double) return value */
24fd0c672fSmrg
25fd0c672fSmrg{
26fd0c672fSmrg  *(double *)call_data = 1.0;
27fd0c672fSmrg}
28fd0c672fSmrg#else  /* RLOADSTUB */
29fd0c672fSmrg
30fd0c672fSmrg#include <protocols/rwhod.h>
31fd0c672fSmrg#ifndef _PATH_RWHODIR
32fd0c672fSmrg#define _PATH_RWHODIR "/var/spool/rwho"
33fd0c672fSmrg#endif
34fd0c672fSmrg
35fd0c672fSmrg#define WHDRSIZE        ((int)(sizeof (buf) - sizeof (buf.wd_we)))
36fd0c672fSmrg
3748e69166Smrgvoid GetRLoadPoint(
3848e69166Smrg    Widget	w,		/* unused */
3948e69166Smrg    XtPointer	closure,	/* unused */
4048e69166Smrg    XtPointer	call_data)	/* pointer to (double) return value */
41fd0c672fSmrg{
42fd0c672fSmrg  int f;
43fd0c672fSmrg  static char *fname = NULL;
44fd0c672fSmrg  static struct whod buf;
45fd0c672fSmrg  int cc;
46fd0c672fSmrg
47fd0c672fSmrg  *(double *)call_data = 0.0; /* to be on the safe side */
48fd0c672fSmrg
49fd0c672fSmrg  if (fname == NULL) {
5083f4f7f0Smrg#ifdef HAVE_ASPRINTF
5183f4f7f0Smrg    if (asprintf(&fname, "%s/whod.%s", _PATH_RWHODIR, resources.remote) < 0) {
5283f4f7f0Smrg      perror("GetRLoadPoint: asprintf() failed");
5383f4f7f0Smrg      exit(1);
5483f4f7f0Smrg    }
5583f4f7f0Smrg#else
56fd0c672fSmrg    if ((fname = malloc(strlen(_PATH_RWHODIR)+strlen("/whod.")+strlen(resources.remote)+1)) == NULL) {
57fd0c672fSmrg      fprintf(stderr,"GetRLoadPoint: malloc() failed\n");
58fd0c672fSmrg      exit(1);
59fd0c672fSmrg    }
60fd0c672fSmrg    strcpy(fname,_PATH_RWHODIR);
61fd0c672fSmrg    strcat(fname,"/whod.");
62fd0c672fSmrg    strcat(fname,resources.remote);
6383f4f7f0Smrg#endif
64fd0c672fSmrg  }
65fd0c672fSmrg  if ((f = open(fname, O_RDONLY, 0)) < 0)
66fd0c672fSmrg    return;
67fd0c672fSmrg
68fd0c672fSmrg  cc = read(f, &buf, sizeof(buf));
69fd0c672fSmrg  close(f);
70fd0c672fSmrg  if (cc < WHDRSIZE)
71fd0c672fSmrg    return;
72fd0c672fSmrg
73fd0c672fSmrg  *(double *)call_data = buf.wd_loadav[0] / 100.0;
74fd0c672fSmrg}
75fd0c672fSmrg
76fd0c672fSmrg#endif  /* RLOADSTUB */
77