-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamonitorpv.c
343 lines (306 loc) · 9.09 KB
/
camonitorpv.c
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
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/*
* $Id$
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/time.h>
#include "cadef.h"
#include "db_access.h"
typedef struct access_rights_handler_args ACCESS_ARGS;
typedef struct connection_handler_args CONNECT_ARGS;
typedef struct exception_handler_args EXCEPT_ARGS;
typedef evargs EVENT_ARGS;
#define REALLY_SMALL 0.0000001
typedef void (*OLD_SIG_FUNC)(int);
fd_set all_fds;
int do_not_exit=1;
int never_connected=1;
char pv_name[50];
char pv_value[50];
char* script_name;
int pv_type;
chid id;
evid event;
static void conCB(CONNECT_ARGS args);
static void exCB(EXCEPT_ARGS args);
static void evCB(evargs args);
static void accCB(ACCESS_ARGS args);
static void fdCB(void* ua, int fd, int opened);
static void getCB(EVENT_ARGS args);
static void sig_func(int x)
{
do_not_exit=0;
return;
}
static void sig_chld(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
signal(SIGCHLD,sig_chld);
}
int main(int argc, char** argv)
{
fd_set rfds;
int tot;
struct timeval tv;
if(argc!=3)
{
fprintf(stderr,"Usage: %s PV_to_monitor script_to_run\n\n",argv[0]);
fprintf(stderr,"This program monitored PV PV_to_monitor and\n");
fprintf(stderr,"runs and executes script_to_run, which can be\n");
fprintf(stderr,"any program or executable script.\n");
fprintf(stderr,"The script or program gets invoked with the first\n");
fprintf(stderr,"argument as the PV name and the second argument\n");
fprintf(stderr,"as the value of the PV\n");
fprintf(stderr,"The program or shell script script_to_run is\n");
fprintf(stderr,"run as a separate child process of this program\n");
fprintf(stderr,"This means that your script or program will not\n");
fprintf(stderr,"stop this process from running, if fact your\n");
fprintf(stderr,"script or program can be invoked many times if\n");
fprintf(stderr,"the value of the PV is changing rapidly and\n");
fprintf(stderr,"several instances of your program could be running\n");
fprintf(stderr,"simultaneously.\n");
return -1;
}
strcpy(pv_name,argv[1]);
script_name=argv[2];
pv_value[0]='\0';
if(access(script_name,X_OK)<0)
{
fprintf(stderr,"Script %s not found or not executable\n",script_name);
return -1;
}
signal(SIGINT,sig_func);
signal(SIGQUIT,sig_func);
signal(SIGTERM,sig_func);
signal(SIGHUP,sig_func);
signal(SIGCHLD,sig_chld);
FD_ZERO(&all_fds);
SEVCHK(ca_task_initialize(),"task initialize");
SEVCHK(ca_add_fd_registration(fdCB,&all_fds),"add fd registration");
SEVCHK(ca_add_exception_event(exCB,NULL),"add exception event");
SEVCHK(ca_search_and_connect(pv_name,&id,conCB,NULL),
"search and connect");
SEVCHK(ca_replace_access_rights_event(id,accCB),
"replace access rights event");
/* SEVCHK(ca_add_event(DBR_TIME_STRING,data.id,evCB,&data,&data.event),
"add event"); */
ca_pend_event(REALLY_SMALL);
/* fprintf(stderr,"Monitoring <%s>\n",pv_name); */
while(do_not_exit)
{
rfds=all_fds;
tv.tv_sec=1;
tv.tv_usec=0; /*200000*/;
switch(tot=select(FD_SETSIZE,&rfds,NULL,NULL,&tv))
{
/*
case -1:
perror("select error - bad");
break;
*/
case 0:
ca_pend_event(REALLY_SMALL);
break;
default:
/* fprintf(stderr,"select data ready\n"); */
ca_pend_event(REALLY_SMALL);
break;
}
}
fprintf(stderr,"PV monitor program is exiting!\n");
ca_task_exit();
return 0;
}
static void fdCB(void* ua, int fd, int opened)
{
fd_set* fds = (fd_set*)ua;
/* fprintf(stderr,"fdCB: openned=%d, fd=%d\n",opened,fd); */
if(opened)
FD_SET(fd,fds);
else
FD_CLR(fd,fds);
}
static void conCB(CONNECT_ARGS args)
{
/* void* node=(void*)ca_puser(args.chid); */
long rc;
/*
fprintf(stderr,"exCB: -------------------------------\n");
fprintf(stderr,"conCB: name=%s\n",ca_name(args.chid));
fprintf(stderr,"conCB: type=%d\n",ca_field_type(args.chid));
fprintf(stderr,"conCB: number elements=%d\n",ca_element_count(args.chid));
fprintf(stderr,"conCB: host name=%s\n",ca_host_name(args.chid));
fprintf(stderr,"conCB: read access=%d\n",ca_read_access(args.chid));
fprintf(stderr,"conCB: write access=%d\n",ca_write_access(args.chid));
fprintf(stderr,"conCB: state=%d\n",ca_state(args.chid));
*/
if(ca_state(args.chid)==cs_conn)
{
if (never_connected) {
never_connected=0;
/* issue a get */
if(ca_field_type(args.chid)==DBF_ENUM)
{
pv_type=DBF_ENUM;
rc=ca_array_get_callback(DBR_GR_ENUM,1,id,getCB,NULL);
}
else
{
pv_type=DBF_STRING;
rc=ca_array_get_callback(DBR_STRING,1,id,getCB,NULL);
}
SEVCHK(rc,"get with callback bad");
}
}
else
fprintf(stderr,"PV <%s> not connected\n",pv_name);
}
static void getCB(EVENT_ARGS args)
{
/* void* node=(void*)ca_puser(args.chid); */
struct dbr_gr_enum* edb;
char* cdb;
if(args.status==ECA_NORMAL)
{
if(pv_type==DBF_ENUM)
{
edb=(struct dbr_gr_enum*)args.dbr;
cdb=&edb->strs[edb->value][0];
/* strncpy(pv_value,cdb,MAX_ENUM_STRING_SIZE); */
SEVCHK(ca_add_event(DBR_GR_ENUM,id,evCB,NULL,&event),
"add event");
}
else
{
cdb=(char*)args.dbr;
/* strncpy(pv_value,cdb,MAX_STRING_SIZE); */
SEVCHK(ca_add_event(DBR_STRING,id,evCB,NULL,&event),
"add event");
}
}
else
fprintf(stderr,"PV <%s> get failed\n",pv_name);
}
static void exCB(struct exception_handler_args args)
{
#define MAX_EXCEPTIONS 25
static int nexceptions=0;
static int ended=0;
if(ended) return;
if(nexceptions++ > MAX_EXCEPTIONS) {
ended=1;
fprintf(stderr,"exCB Exception: Channel Access Exception:\n"
"Too many exceptions [%d]\n"
"No more will be handled\n"
"Please fix the problem and restart camonitorpv",
MAX_EXCEPTIONS);
ca_add_exception_event(NULL, NULL);
return;
}
fprintf(stderr,"exCB Exception: Channel Access Exception:\n"
" Channel Name: %s\n"
" Native Type: %s\n"
" Native Count: %lu\n"
" Access: %s%s\n"
" IOC: %s\n"
" Message: %s\n"
" Context: %s\n"
" Requested Type: %s\n"
" Requested Count: %ld\n"
" Source File: %s\n"
" Line number: %u",
args.chid?ca_name(args.chid):"Unavailable",
args.chid?dbf_type_to_text(ca_field_type(args.chid)):"Unavailable",
args.chid?ca_element_count(args.chid):0,
args.chid?(ca_read_access(args.chid)?"R":""):"Unavailable",
args.chid?(ca_write_access(args.chid)?"W":""):"",
args.chid?ca_host_name(args.chid):"Unavailable",
ca_message(args.stat)?ca_message(args.stat):"Unavailable",
args.ctx?args.ctx:"Unavailable",
dbf_type_to_text(args.type),
args.count,
args.pFile?args.pFile:"Unavailable",
args.pFile?args.lineNo:0);
}
static void evCB(evargs args)
{
/* void* p = (void*)ca_puser(args.chid); */
struct dbr_gr_enum* edb;
char* cdb;
int len;
if(args.status!=ECA_NORMAL)
{
/*
fprintf(stderr,"evCB: ------NOT-NORMAL---------------\n");
fprintf(stderr,"evCB: name=%s\n",ca_name(args.chid));
fprintf(stderr,"evCB: type=%d\n",ca_field_type(args.chid));
fprintf(stderr,"evCB: number ele=%d\n",ca_element_count(args.chid));
fprintf(stderr,"evCB: host name=%s\n",ca_host_name(args.chid));
fprintf(stderr,"evCB: read access=%d\n",ca_read_access(args.chid));
fprintf(stderr,"evCB: write access=%d\n",ca_write_access(args.chid));
fprintf(stderr,"evCB: state=%d\n",ca_state(args.chid));
*/
fprintf(stderr,"Event receive failure for <%s>\n",pv_name);
}
else
{
/* fprintf(stderr,"evCB: %s=%s\n",ca_name(args.chid),args.dbr); */
if(pv_type==DBF_ENUM)
{
edb=(struct dbr_gr_enum*)args.dbr;
cdb=&edb->strs[edb->value][0];
len=MAX_ENUM_STRING_SIZE;
}
else
{
cdb=(char*)args.dbr;
len=MAX_STRING_SIZE;
}
if(strcmp(pv_value,cdb)!=0)
{
strncpy(pv_value,cdb,len);
/* run the script now */
switch(fork())
{
case -1: /* error */
perror("Cannot create gateway processes");
break;
case 0: /* child */
/* script exec */
execlp(script_name,"user_script",pv_name,pv_value,NULL);
perror("Execute of script failed");
exit(1);
break;
default: /* parent */
break;
}
}
}
}
static void accCB(ACCESS_ARGS args)
{
/*
fprintf(stderr,"accCB: -------------------------------\n");
fprintf(stderr,"accCB: name=%s\n",ca_name(args.chid));
fprintf(stderr,"accCB: type=%d\n",ca_field_type(args.chid));
fprintf(stderr,"accCB: number elements=%d\n",ca_element_count(args.chid));
fprintf(stderr,"accCB: host name=%s\n",ca_host_name(args.chid));
fprintf(stderr,"accCB: read access=%d\n",ca_read_access(args.chid));
fprintf(stderr,"accCB: write access=%d\n",ca_write_access(args.chid));
fprintf(stderr,"accCB: state=%d\n",ca_state(args.chid));
*/
}