File: module.c

package info (click to toggle)
procmeter3 3.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,444 kB
  • sloc: ansic: 16,600; makefile: 428; sh: 13
file content (451 lines) | stat: -rw-r--r-- 10,043 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/***************************************
  ProcMeter - A system monitoring program for Linux - Version 3.5d.

  Module handling functions.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1998-2011 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>

#include <dlfcn.h>

#include "procmeter.h"
#include "procmeterp.h"

/*+ The list of all of the modules. +*/
Module *Modules=NULL;


/*++++++++++++++++++++++++++++++++++++++
  Load in all of the modules in the library directory.
  ++++++++++++++++++++++++++++++++++++++*/

void LoadAllModules(void)
{
 DIR *dir;
 struct dirent* ent;
 struct stat buf;
 char lib[PATH_MAX+1],*libp;
 char *library;
 int i;

 Modules=(Module*)malloc(16*sizeof(Module));
 *Modules=NULL;

 /* set LC_NUMERIC to C, else 0.1 in /proc/loadavg may not be parsed */

 setlocale(LC_NUMERIC, "C");

 /* Load the library modules */

 if(!(library=GetProcMeterRC("library","path")))
    library=MOD_PATH;

 strcpy(lib,library);
 libp=lib+strlen(lib)-1;
 if(*libp!='/')
    *++libp='/';
 *++libp=0;

 if(stat(library,&buf) || !S_ISDIR(buf.st_mode))
   {
    fprintf(stderr,"ProcMeter: The library directory '%s' does not exist or is not a directory.\n",library);
    exit(1);
   }

 dir=opendir(library);
 if(!dir)
   {
    fprintf(stderr,"ProcMeter: The library directory '%s' cannot be opened.\n",library);
    exit(1);
   }

 while((ent=readdir(dir)))
    if(!strcmp(ent->d_name+strlen(ent->d_name)-3,".so"))
      {
       strcpy(libp,ent->d_name);
       LoadModule(lib);
      }

 closedir(dir);

 /* Load the other modules. */

 if((library=GetProcMeterRC("library","others")))
   {
    char *l=library;

    if(!getcwd(lib,PATH_MAX))
      {
       fprintf(stderr,"ProcMeter: Cannot get current directory name.\n");
       exit(1);
      }

    libp=lib+strlen(lib)-1;
    if(*libp!='/')
       *++libp='/';
    *++libp=0;

    while(*l && *l==' ')
       l++;

    while(*l)
      {
       char *r=l,pr;

       while(*r && *r!=' ')
          r++;

       pr=*r;
       *r=0;

       if(*l=='/')
          LoadModule(l);
       else
         {
          strcpy(libp,l);
          LoadModule(lib);
         }

       *r=pr;
       while(*r && *r==' ')
          r++;

       if(!*r)
          break;

       l=r;
      }
   }

 /* Add to the menus */

 for(i=0;Modules[i];i++)
    AddModuleToMenu(Modules[i]);

}


/*++++++++++++++++++++++++++++++++++++++
  Unload all of the modules.
  ++++++++++++++++++++++++++++++++++++++*/

void UnloadAllModules(void)
{
 while(*Modules)
   {
    UnloadModule(*Modules);
   }

 free(Modules);
}


/*++++++++++++++++++++++++++++++++++++++
  Load the specified module.

  Module LoadModule Returns a pointer to the new module structure, or NULL on error.

  char* filename The filename of the module to load.
  ++++++++++++++++++++++++++++++++++++++*/

Module LoadModule(char* filename)
{
 ProcMeterModule *(*Load)(void);
 ProcMeterOutput **(*Initialise)(char *),**outputs,*output;
 Module new=NULL;
 int noutputs,i,m,c;

 new=(Module)malloc(sizeof(struct _Module));

 new->filename=(char*)malloc(strlen(filename)+1);
 strcpy(new->filename,filename);

 new->dlhandle=dlopen(filename,RTLD_NOW);

 if(!new->dlhandle)
   {
    fprintf(stderr,"ProcMeter: Cannot open the module '%s' : %s\n",new->filename,dlerror());
    free(new->filename);
    free(new);
    return(NULL);
   }

 /* Get the Load() function and call it */

 Load=dlsym(new->dlhandle,"Load");

 if(!Load)
   {
    fprintf(stderr,"ProcMeter: Cannot get the symbol 'Load' from '%s' : %s\n",new->filename,dlerror());
    dlclose(new->dlhandle);
    free(new->filename);
    free(new);
    return(NULL);
   }

 if(!(new->module=(*Load)()))
   {
    fprintf(stderr,"ProcMeter: Error calling 'Load()' in module '%s'\n",new->filename);
    dlclose(new->dlhandle);
    free(new->filename);
    free(new);
    return(NULL);
   }

 for(m=-1,i=0;Modules[i];i++)
    if(!(c=strcmp(Modules[i]->module->name,new->module->name)))
      {
       fprintf(stderr,"ProcMeter: Duplicate module name '%s' ignoring '%s'.\n",new->module->name,new->filename);
       dlclose(new->dlhandle);
       free(new->filename);
       free(new);
       return(NULL);
      }
    else if(c<0)
       m=i;

 /* Get the Intialise() fuction and call it. */

 Initialise=dlsym(new->dlhandle,"Initialise");

 if(!Initialise)
   {
    fprintf(stderr,"ProcMeter: Cannot get the symbol 'Initialise' from '%s' : %s\n",new->filename,dlerror());
    dlclose(new->dlhandle);
    free(new->filename);
    free(new);
    return(NULL);
   }

 if(!(outputs=(*Initialise)(GetProcMeterRC(new->module->name,"options"))))
   {
    fprintf(stderr,"ProcMeter: Error Calling 'Initialise()' in module '%s'\n",new->filename);
    dlclose(new->dlhandle);
    free(new->filename);
    free(new);
    return(NULL);
   }

 /* Get the Update() function. */

 new->Update=dlsym(new->dlhandle,"Update");

 if(!new->Update)
   {
    fprintf(stderr,"ProcMeter: Cannot get the symbol 'Update' from '%s' : %s\n",new->filename,dlerror());
    dlclose(new->dlhandle);
    free(new->filename);
    free(new);
    return(NULL);
   }

 /* Insert the new module. */

 Modules=(Module*)realloc((void*)Modules,(i+2)*sizeof(Module));
 memmove(Modules+m+2, Modules+m+1, (i-m)*sizeof(Module));
 Modules[m+1]=new;

 /* Add the outputs */

 for(noutputs=0,i=0,output=outputs[i];output;output=outputs[++i])
   {
    int t=output->type;

    while(t)
      {
       if(t&1)
          noutputs++;
       t>>=1;
      }
   }

 new->outputs=(Output*)malloc((noutputs+1)*sizeof(Output));

 new->outputs[noutputs]=NULL;

 for(noutputs=0,i=0,output=outputs[i];output;output=outputs[++i])
   {
    int t=1;

    while(t<=output->type)
      {
       if(output->type&t)
         {
          char *string;

          if((string=GetProcMeterRC2(new->module->name,output->name,"update")) ||
             (string=GetProcMeterRC(new->module->name,"update")) ||
             (string=GetProcMeterRC("resources","update")))
             output->interval=atoi(string);

          if((string=GetProcMeterRC2(new->module->name,output->name,"graph-scale")) ||
             (string=GetProcMeterRC(new->module->name,"graph-scale")))
             output->graph_scale=atoi(string);

          new->outputs[noutputs]=(Output)malloc(sizeof(struct _Output));
          new->outputs[noutputs]->output=output;
          new->outputs[noutputs]->type=t;
          new->outputs[noutputs]->first=0;
          new->outputs[noutputs]->menu_item_widget=NULL;
          new->outputs[noutputs]->output_widget=NULL;

          if(!(string=GetProcMeterRC2(new->module->name,output->name,"run")))
             string=GetProcMeterRC(new->module->name,"run");
          ParseRunCommand(string,&new->outputs[noutputs]->menu_run);

          if(!(string=GetProcMeterRC2(new->module->name,output->name,"label")))
            {
             static char newstr[PROCMETER_NAME_LEN+1];
             int i;
             for(i=0;output->name[i];i++)
                if(output->name[i]=='_')
                   newstr[i]=' ';
                else
                   newstr[i]=output->name[i];
             newstr[i]=0;
             string=newstr;
            }

          strncpy(new->outputs[noutputs]->label,string,PROCMETER_NAME_LEN+1);
          new->outputs[noutputs]->label[PROCMETER_NAME_LEN]=0;

          noutputs++;
         }

       t<<=1;
      }
   }

 new->menu_item_widget=NULL;
 new->submenu_widget=NULL;

 return(new);
}


/*++++++++++++++++++++++++++++++++++++++
  Unload a module when we exit.

  Module module The module that is to be unloaded.
  ++++++++++++++++++++++++++++++++++++++*/

void UnloadModule(Module module)
{
 void (*Unload)(void);
 Module *modules;

 RemoveModuleFromMenu(module);

 Unload=dlsym(module->dlhandle,"Unload");

 if(Unload)
    (*Unload)();

 dlclose(module->dlhandle);

 /* Shuffle the modules down and tidy up. */

 modules=Modules;
 while(*modules)
   {
    if(*modules==module)
       while(*modules)
         {
          *modules=*(modules+1);
          modules++;
         }
    else
       modules++;
   }

 if(module->outputs)
   {
    int i;
    Output output;

    for(output=module->outputs[i=0];output;output=module->outputs[++i])
      {
       if(output->menu_run.command)
          free(output->menu_run.command);
       free(output);
      }
    free(module->outputs);
   }

 free(module->filename);
 free(module);
}


#define INCSIZE 256             /*+ The buffer increment size +*/

/*++++++++++++++++++++++++++++++++++++++
  Call fgets and realloc the buffer as needed to get a whole line.

  char *fgets_realloc Returns the modified buffer (NULL at the end of the file).

  char **buffer A pointer to the location of the buffer (pointer to NULL to intialise a new one).

  size_t *length A pointer to the current length of the buffer or the intial length if buffer is NULL.

  FILE *file The file to read from.
  ++++++++++++++++++++++++++++++++++++++*/

char *fgets_realloc(char **buffer,size_t *length,FILE *file)
{
 int n=0;
 char *buf;

 if(!*buffer)
   {
    if(!*length)
       *length=INCSIZE;

    *buffer=(char*)malloc(*length);

    if(!*buffer)
      {*length=0;return(NULL);}
   }

 while((buf=fgets(*buffer+n,*length-n,file)))
   {
    int s=strlen(buf);
    n+=s;

    if((*buffer)[n-1]=='\n')
       break;
    else
      {
       char *newbuffer;

       *length+=INCSIZE;
       newbuffer=(char*)realloc(*buffer,*length);

       if(!newbuffer)
         {free(*buffer);*buffer=NULL;*length=0;return(NULL);}
       else
          *buffer=newbuffer;
      }
   }

 if(!buf)
   {**buffer=0;return(NULL);}
 else
    return(*buffer);
}