-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.c
261 lines (213 loc) · 6.83 KB
/
process.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
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* Copyright (c) 2002 Berliner Speicherring-Gesellschaft fuer Synchrotron-
* Strahlung mbH (BESSY).
* Copyright (c) 2002 Southeastern Universities Research Association, as
* Operator of Thomas Jefferson National Accelerator Facility.
* 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.
\*************************************************************************/
/* process.c */
/************************DESCRIPTION***********************************
This file contains routines for spawning a related process
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WIN32
#include <process.h> /* For spawn */
#endif
#include <Xm/Xm.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include "alLib.h"
#include "ax.h"
#define masterCommand "MASTER_ONLY"
extern int DEBUG;
extern int _lock_flag;
extern int masterFlag;
extern int notsave;
static char *g_popupCmdString;
static Widget g_pum = NULL;
#ifdef WIN32
#define strtok_r strtok_s
#endif
/******************************************************
spawn a new related prcess if command is not null
******************************************************/
void processSpawn_callback(Widget w,char *command,void * call_data)
{
char buff[MAX_STRING_LENGTH +2];
size_t l;
int status;
#ifdef WIN32
static int first=1;
static char *ComSpec;
#endif
if(notsave) { fprintf(stderr,"NOT SAVE mode - no related process started"); return;}
if (command) {
/* Strip any LF from the end */
l = strlen(command);
if (*(command+l-1) == '\n') *(command+l-1) = ' ';
/* If more then 1 alh process work with the same config files
of cource, usually all this process can span all callback,
BUT in some special case (in our situation it's mail to
mobil-phone) it's expensive (in money :) or processor-time)
or not so important. In this case ONLY master alh_process
span this task. For distinguish with common case we add
additional last parameters in command MASTER_ONLY:
WAS: $SEVRCOMMAND UP_ERROR mailx [email protected] "ALARM HAPPEN"
NOW: $SEVRCOMMAND UP_ERROR MASTER_ONLY mailx [email protected] "ALARM HAPPEN"
Coding:
------
If don't locking system -- cut MASTER_ONLY from string and spawn it
Else if you're master-alh-process -- the same
else do nothing
*/
/* ________MASTER_ONLY code ________________________ */
if(strncmp(command,masterCommand,strlen(masterCommand))==0)
{
command += strlen(masterCommand);
if (_lock_flag && !masterFlag) return;
}
/* _______End MASTER_ONLY code ______________________ */
#ifdef WIN32
sprintf(buff,"%s",command);
/* Get ComSpec for the command shell (should be defined) */
if (first) {
first=0;
ComSpec = getenv("ComSpec");
}
if (!ComSpec) {
errMsg("processSpawn_callback: Cannot find command processor\n");
return;
}
status = (int)_spawnl(_P_DETACH, ComSpec, ComSpec, "/C", buff, NULL);
#else
sprintf(buff,"%s &",command);
status=system(buff);
#endif
if(status == -1) {
/* System call failed */
char *errstring=strerror(errno);
errMsg("processSpawn_callback: Cannot process command:\n"
"%s\n %s",
buff,errstring);
} else if (status > 0) {
/* Assume program returned an error */
errMsg("processSpawn_callback: Command returned %d:\n"
"%s\n",
status,buff);
}
}
}
/***************************************************
menu callback
****************************************************/
static void relprocmenu_cb (
Widget w,
XtPointer index,
void * call_data
) {
char buf[1023+1], *tk, *ctx;
int i;
int indexi = (long) index;
strncpy( buf, g_popupCmdString, 1023 );
buf[1023] = 0;
i = 0;
ctx = NULL;
tk = strtok_r( buf, "!\n", &ctx );
while ( tk ) {
tk = strtok_r( NULL, "!\n", &ctx );
if ( tk ) {
if ( i == indexi ) {
processSpawn_callback( w, tk, call_data );
return;
}
i++;
}
tk = strtok_r( NULL, "!\n", &ctx );
};
}
/***************************************************
relatedProcess_callback
****************************************************/
void relatedProcess_callback(void *widget,GCLINK *link,void *cbs)
{
void *area;
char buf[1023+1], *tk, *ctx;
int i, n;
Widget pdm, pb;
Arg args[10];
XmString str;
Window root, child;
int rootX, rootY, winX, winY;
unsigned int mask;
XButtonEvent be;
XtVaGetValues(widget, XmNuserData, &area, NULL);
if (link && alProcessExists(link)){
strncpy( buf, link->pgcData->command, 1023 );
buf[1023] = 0;
i = 0;
ctx = NULL;
tk = strtok_r( buf, "!\n", &ctx );
while ( tk ) {
if ( tk ) {
i++;
}
tk = strtok_r( NULL, "!\n", &ctx );
};
if ( i > 1 ) {
if ( i % 2 ) { /* if i > one then i must be even */
fprintf( stderr, "Command syntax error\n" );
return;
}
g_popupCmdString = link->pgcData->command;
if ( g_pum ) {
XtDestroyWidget( g_pum );
g_pum = NULL;
}
n = 0;
XtSetArg( args[n], XmNpopupEnabled, (XtArgVal) False ); n++;
g_pum = XmCreatePopupMenu( topLevelShell, "relprocmenu", args, n );
pdm = XmCreatePulldownMenu( g_pum, "relprocpd", NULL, 0 );
strncpy( buf, link->pgcData->command, 1023 );
buf[1023] = 0;
i = 0;
ctx = NULL;
tk = strtok_r( buf, "!\n", &ctx );
while ( tk ) {
if ( tk ) {
long ilong = i;
str = XmStringCreateLocalized( tk );
pb = XtVaCreateManagedWidget( "pb", xmPushButtonWidgetClass,
g_pum, XmNlabelString, str, NULL );
XmStringFree( str );
XtAddCallback( pb, XmNactivateCallback, relprocmenu_cb,
(XtPointer)ilong );
i++;
}
tk = strtok_r( NULL, "!\n", &ctx );
tk = strtok_r( NULL, "!\n", &ctx );
};
XQueryPointer( display, XtWindow(XtParent(widget)), &root, &child,
&rootX, &rootY, &winX, &winY, &mask );
be.x_root = rootX;
be.y_root = rootY;
XmMenuPosition( g_pum, &be );
XtManageChild( g_pum );
}
else {
strncpy( buf, link->pgcData->command, 1023 );
buf[1023] = 0;
ctx = NULL;
tk = strtok_r( buf, "!\n", &ctx );
processSpawn_callback( widget, tk, cbs );
}
}
}