~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to monit/prepz2.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
  Copyright (C) 1995-2010 European Southern Observatory (ESO)
 
3
 
 
4
  This program is free software; you can redistribute it and/or
 
5
  modify it under the terms of the GNU General Public License as
 
6
  published by the Free Software Foundation; either version 2 of
 
7
  the License, or (at your option) any later version.
 
8
 
 
9
  This program is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
  GNU General Public License for more details.
 
13
 
 
14
  You should have received a copy of the GNU General Public
 
15
  License along with this program; if not, write to the Free
 
16
  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
 
17
  MA 02139, USA.
 
18
 
 
19
  Correspondence concerning ESO-MIDAS should be addressed as follows:
 
20
        Internet e-mail: midas@eso.org
 
21
        Postal address: European Southern Observatory
 
22
                        Data Management Division
 
23
                        Karl-Schwarzschild-Strasse 2
 
24
                        D 85748 Garching bei Muenchen
 
25
                        GERMANY
 
26
===========================================================================*/
 
27
 
 
28
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
29
 
 
30
.LANGUAGE      C
 
31
.IDENT          MIDAS Monitor module prepz2.c
 
32
.KEYWORDS
 
33
  MIDAS monitor, process control
 
34
.COMMENTS
 
35
  holds hostinfo, ospsysinfo, pipe_mess
 
36
.VERSION
 
37
 
 
38
 100907         last modif      
 
39
-----------------------------------------------------------------------------*/
 
40
 
 
41
#include <fileexts.h>
 
42
#include <monitdef.h>
 
43
#include <midback.h>
 
44
 
 
45
#include <stdlib.h>
 
46
#include <stdio.h>
 
47
#include <string.h>
 
48
 
 
49
#include <math.h>
 
50
 
 
51
/*
 
52
#include <sys/utsname.h>
 
53
*/
 
54
 
 
55
#define MAX_CBUF  4000
 
56
 
 
57
 
 
58
 
 
59
 
 
60
 
 
61
/* hostinfo: return useful system info */
 
62
 
 
63
int hostinfo(prf,sizes,node,mach,opsys)
 
64
 
 
65
int  prf;       /* IN: print flag (1 = yes) */
 
66
int *sizes;     /* OUT: 7 elements to get size of
 
67
                        short int, int, long int,
 
68
                        float, double,
 
69
                        pointer, off_t, size_t */
 
70
 
 
71
char *node;     /* OUT: name of computer */
 
72
char *mach;     /* OUT: machine architecture */
 
73
char *opsys;    /* OUT: host system */
 
74
 
 
75
{
 
76
short int sm;
 
77
int  m, fid, svp, sst;
 
78
long int lm;
 
79
unsigned long int ulm;
 
80
 
 
81
float f;
 
82
 
 
83
double d;
 
84
 
 
85
size_t t;
 
86
off_t offset;
 
87
 
 
88
void *vp;
 
89
 
 
90
char   *workdir, filename[132], cbuf[144], myunit[4];
 
91
 
 
92
/*
 
93
struct utsname *sysbuf;
 
94
*/
 
95
 
 
96
 
 
97
 
 
98
 
 
99
 
 
100
*sizes = sizeof(sm);
 
101
*(sizes+1) = sizeof(m);
 
102
*(sizes+2) = sizeof(lm);
 
103
*(sizes+3) = sizeof(ulm);
 
104
*(sizes+4) = sizeof(f);
 
105
*(sizes+5) = sizeof(d);
 
106
*(sizes+6) = svp = sizeof(vp);
 
107
*(sizes+7) = sst = sizeof(t);
 
108
*(sizes+8) = sizeof(offset);
 
109
 
 
110
if (svp == 4)
 
111
   {
 
112
   KCWORDS[OFF_SYS+30] = '3';
 
113
   KCWORDS[OFF_SYS+31] = '2';
 
114
   }
 
115
else
 
116
   {
 
117
   KCWORDS[OFF_SYS+30] = '6';
 
118
   KCWORDS[OFF_SYS+31] = '4';
 
119
   }
 
120
m = OFF_AUX + 18;                               /* point to AUX_MODE(19) */
 
121
KIWORDS[m++] = sst * 8;                         /* 32 or 64 */
 
122
 
 
123
t = (size_t) pow((double)2,(double)(31));       /* store in AUX_MODE(20) */
 
124
KIWORDS[m] = (int) (t - 1);                     /* max signed int */
 
125
 
 
126
if (prf == 1)
 
127
   { 
 
128
   printf("size of short int = %d, int = %d (bytes)\n",*sizes,*(sizes+1));
 
129
   printf("size of long int = %d, unsigned long int = %d (bytes)\n",
 
130
          *(sizes+2),*(sizes+3));
 
131
   printf("size of float = %d, double = %d (bytes)\n",*(sizes+4),*(sizes+5));
 
132
   printf("size of pointer = %d, size_t = %d, off_t = %d (bytes)\n",
 
133
          svp,*(sizes+7),*(sizes+8));
 
134
   }
 
135
 
 
136
 
 
137
/* get machine hardware name + host system 
 
138
   using the uname() system call leads to segfault on Fedora 6 
 
139
   and also strange behaviour on Suse 11.1 ... 
 
140
   so we do it via: $ uname -mo > file                     */
 
141
 
 
142
#if !defined(__APPLE__)
 
143
(void) strcpy(cbuf,"uname -nmo > ");
 
144
#else                                   /* 'uname -o' not on Mac ... */
 
145
(void) strcpy(cbuf,"uname -nma > ");
 
146
#endif
 
147
 
 
148
if (!(workdir = getenv("MID_WORK")))
 
149
   {
 
150
   if (!(workdir = getenv("HOME"))) return (-1);
 
151
 
 
152
   (void) strcpy(filename,workdir);
 
153
   (void) strcat(filename,"/midwork");
 
154
   }
 
155
else
 
156
   (void) strcpy(filename,workdir);
 
157
 
 
158
myunit[0] = FRONT.DAZUNIT[0];
 
159
myunit[1] = FRONT.DAZUNIT[1];
 
160
myunit[2] = '\0';
 
161
(void) strcat(filename,"/Midas_info");
 
162
(void) strcat(filename,myunit);         /* filename: $MID_WORK/Midas_infoXY */
 
163
 
 
164
(void) strcat(cbuf,filename);
 
165
(void) system((const char *)cbuf);                      /* execute the $uname command */
 
166
 
 
167
fid = osaopen(filename,READ);
 
168
if (fid < 0) return (-1);
 
169
 
 
170
m = osaread(fid,cbuf,40);
 
171
osaclose(fid);
 
172
if (m < 0) return (-1);                 /* could not read the file... */
 
173
 
 
174
m = CGN_INDEXC(cbuf,' ');       /* we should have the string: */
 
175
if (m < 0) return (-1);         /* "node mach opsys"          */   
 
176
 
 
177
cbuf[m] = '\0';
 
178
(void) strcpy(node,cbuf);
 
179
CGN_strcpy(cbuf,cbuf+m+1);
 
180
 
 
181
m = CGN_INDEXC(cbuf,' ');
 
182
if (m < 0) return (-1);
 
183
 
 
184
cbuf[m] = '\0';
 
185
(void) strcpy(mach,cbuf);
 
186
(void) strcpy(opsys,cbuf+m+1);
 
187
 
 
188
if (prf == 1)
 
189
   {
 
190
   printf("name: %s\n",node);
 
191
   printf("architecture: %s\n",mach);
 
192
   printf("hostsys: %s (%c%c bit)\n",opsys,
 
193
                    KCWORDS[OFF_SYS+30],KCWORDS[OFF_SYS+31]);
 
194
   }
 
195
 
 
196
return 0;
 
197
 
 
198
 
 
199
 
 
200
/* here is the code which fails on some Linux systems...
 
201
 
 
202
m = uname(sysbuf);
 
203
 
 
204
if (m == 0)
 
205
   {
 
206
   (void) strcpy(opsys,sysbuf->sysname);
 
207
   (void) strcpy(mach,sysbuf->machine);
 
208
   if (prf == 1)
 
209
      {
 
210
      printf("sysname: %s\n",sysbuf->sysname);
 
211
      printf("machine: %s\n",sysbuf->machine);
 
212
      printf("release: %s\n",sysbuf->release);
 
213
      printf("version: %s\n",sysbuf->version);
 
214
      }
 
215
   return 0;
 
216
   }
 
217
else
 
218
   {
 
219
   *opsys = '\0';
 
220
   *mach = '\0';
 
221
   return -1;
 
222
   }
 
223
*/
 
224
 
 
225
}
 
226
 
 
227
/*
 
228
 
 
229
*/
 
230
 
 
231
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
232
Put a formatted string to stdout and MIDAS log
 
233
-1: the message identifier is wrong
 
234
0:  no error occured
 
235
implement the pipeline commands
 
236
MESS/OUT and APPEND/OUT
 
237
Put a formatted string to stdout using MID_message
 
238
which in turn calls the message() function
 
239
developed by S. Wolf for the FORS pipeline
 
240
-------------------------------------------------------------*/
 
241
 
 
242
 
 
243
void pipe_mess(actio)
 
244
char  actio;
 
245
 
 
246
{
 
247
static char  *bigbuf;
 
248
 
 
249
int    id, mess_level;
 
250
int    indent, stat, null, unit;
 
251
int    iav;
 
252
static int  cmapped = 0, clen = 0;
 
253
 
 
254
float  rbuf;
 
255
 
 
256
double  dbuf;
 
257
 
 
258
 
 
259
 
 
260
 
 
261
 
 
262
 
 
263
 
 
264
if (cmapped == 0)                       /* allocate once */
 
265
   {
 
266
   bigbuf = (char*) malloc( (size_t)MAX_CBUF);
 
267
   if (bigbuf == (char *) NULL)
 
268
      {
 
269
      (void) SCTPUT("could not allocate memory for MESS_CBUF..");
 
270
      return;
 
271
      }
 
272
   cmapped = 1;
 
273
   clen = 0;
 
274
   }
 
275
 
 
276
 
 
277
 
 
278
/* if actio = A, we do APPEND/MESS */
 
279
 
 
280
if (actio == 'A') 
 
281
   {
 
282
   iav = TOKEN[1].LEN;                          /* process message */
 
283
   if ((TOKEN[1].STR[0] == '"') && (TOKEN[1].STR[iav-1] == '"'))
 
284
      {
 
285
      iav -= 2;
 
286
      (void) memmove(TOKEN[1].STR,&TOKEN[1].STR[1],(size_t) iav);
 
287
      TOKEN[1].STR[iav] = '\0';
 
288
      } 
 
289
 
 
290
   if ((clen + iav) >= MAX_CBUF)
 
291
      { 
 
292
      SCTPUT("overflow in pipeline message buffer...so we flush it first");
 
293
      iav = MID_message(1," ",bigbuf,0);
 
294
      clen = 0;
 
295
      bigbuf[clen] = '\0';
 
296
      }
 
297
 
 
298
   (void) memcpy(bigbuf+clen,TOKEN[1].STR,(size_t)iav);
 
299
   clen += iav;
 
300
   bigbuf[clen] = '\0';
 
301
   return;
 
302
   }
 
303
 
 
304
 
 
305
 
 
306
/* here for MESSAGE/OUT ... */
 
307
 
 
308
id = 0;
 
309
(void) CGN_CNVT(TOKEN[1].STR,1,1,&id,&rbuf,&dbuf);
 
310
if (id < 0) 
 
311
   id = -id;
 
312
else if (id == 0)
 
313
   return;
 
314
 
 
315
stat = SCKRDI("MESS_LEVEL",1,1,&iav,&mess_level,&unit,&null);
 
316
if ((stat == 0) && (id > mess_level))
 
317
   goto end_of_it;              /* that means: clear message buffer */
 
318
 
 
319
 
 
320
indent = 0;
 
321
if (TOKEN[4].STR[0] != '?')
 
322
   (void) CGN_CNVT(TOKEN[4].STR,1,1,&indent,&rbuf,&dbuf);
 
323
 
 
324
iav = TOKEN[3].LEN;                             /* process message */
 
325
if ((TOKEN[3].STR[0] != '?') && (TOKEN[3].STR[0] != '+'))
 
326
   {
 
327
   if ((TOKEN[3].STR[0] == '"') &&
 
328
       (TOKEN[3].STR[iav-1] == '"'))
 
329
      {
 
330
      iav -= 2;
 
331
      (void) memmove(TOKEN[3].STR,&TOKEN[3].STR[1],(size_t) iav);
 
332
      TOKEN[3].STR[iav] = '\0';
 
333
      }
 
334
 
 
335
   if ((clen + iav) >= MAX_CBUF)
 
336
     (void) SCTPUT("overflow of pipeline message buffer...");
 
337
   else
 
338
      {
 
339
      (void) memcpy(bigbuf+clen,TOKEN[3].STR,(size_t)iav);
 
340
      clen += iav;
 
341
      bigbuf[clen] = '\0';
 
342
      } 
 
343
   }
 
344
 
 
345
 
 
346
/* process + display the complete message buffer */
 
347
 
 
348
iav = MID_message(id,TOKEN[2].STR,bigbuf,indent);    
 
349
if (iav < 0) printf("`message' returned %d\n",iav);
 
350
 
 
351
 
 
352
end_of_it:
 
353
clen = 0;                       /* clear buffer again */
 
354
bigbuf[0] = '\0';
 
355
}
 
356