This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsiutil.c
385 lines (345 loc) · 8.49 KB
/
siutil.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
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
#include "libyasdi.h"
#include "libyasdimaster.h"
#include <ctype.h>
#include <sys/types.h>
//WChanType = 0x040f and bChanIndex = 0 denotes all parameter channels
//WChanType = 0x090f and bChanIndex = 0 denotes all spot value channels
#define CHANTYPE_PARM 0x040f
#define CHANTYPE_SPOT 0x090f
#define CHANTYPE_ALL 0xffff
#define MAX_CHANHANDLES 2048
#ifdef DEBUG
#define dprintf(format, args...) printf("%s(%d): " format,__FUNCTION__,__LINE__, ## args)
#else
#define dprintf(format, args...) /* noop */
#endif
extern BOOL TSecurity_SetNewAccessLevel(char * user, char * passwd);
int dolist(int handle, int type, char *chanName) {
DWORD ChanHandles[MAX_CHANHANDLES];
char valtext[32],name[32];
int r,i,count;
dprintf("handle: %d, type: %x, chanName: %s\n", handle, type, chanName);
/* If name specified, list possible options for chan */
if (chanName) {
int chan;
char valtext[32];
chan = FindChannelName(handle, chanName);
dprintf("chan: %d\n", chan);
if (chan < 1) {
printf("error: channel name not found\n");
return 1;
}
count = GetChannelStatTextCnt(chan);
dprintf("count: %d\n", count);
if (!count) {
printf("channel has no option names\n");
return 0;
}
for(i=0; i < count; i++) {
r = GetChannelStatText(chan, i, valtext, sizeof(valtext)-1);
dprintf("r: %d\n", r);
if (r < 0) {
printf("error: unable to get channel option\n");
return 1;
}
dprintf("i: %d, text: %s\n", i, valtext);
printf("%s\n",valtext);
}
} else {
memset(valtext,0,sizeof(valtext));
r = GetChannelHandles(handle, ChanHandles, MAX_CHANHANDLES, type, 0);
printf("r: %d\n", r);
if (r < 0) return 1;
count = r;
for(i=0; i < count; i++) {
r = GetChannelName(ChanHandles[i], name, sizeof(name)-1);
if (r < 0) {
printf("list: error getting channel name for channel %d!\n",i);
return 1;
}
printf("%d: %s\n", i, name);
}
}
return 0;
}
int getparm(int handle, char *name) {
int chan,r;
char valtext[32];
double cval;
dprintf("name: %s\n", name);
chan = FindChannelName(handle, name);
dprintf("chan: %d\n", chan);
if (chan < 1) {
printf("error: channel name not found\n");
return 1;
}
memset(valtext,0,sizeof(valtext));
r = GetChannelValue(chan,handle,&cval,valtext,sizeof(valtext)-1,99);
dprintf("r: %d\n", r);
if (r < 0) {
printf("error: unable to get current value\n");
return 1;
}
dprintf("cval: %f, valtext: %s\n", cval, valtext);
dprintf("%d: %s (%f)\n", chan, valtext, cval);
if (strlen(valtext))
printf("%s\n",valtext);
else
printf("%f\n",cval);
return 0;
}
int setparm(int handle, char *name, char *sval) {
int i,chan,count,r,found;
char valtext[32];
double cval,dval;
dprintf("name: %s, sval: %s\n", name, sval);
chan = FindChannelName(handle, name);
dprintf("chan: %d\n", chan);
if (chan < 0) {
printf("error: param not found\n");
return 1;
}
memset(valtext,0,sizeof(valtext));
r = GetChannelValue(chan,handle,&cval,valtext,sizeof(valtext)-1,99);
dprintf("r: %d\n", r);
if (r < 0) {
printf("error: unable to get current value\n");
return 1;
}
dprintf("cval: %f, valtext: %s\n", cval, valtext);
count = GetChannelStatTextCnt(chan);
dprintf("count: %d\n", count);
if (count) {
dprintf("current value: %s\n", valtext);
if (strcmp(sval,valtext)==0) return 0;
found = 0;
for(i=0; i < count; i++) {
r = GetChannelStatText(chan, i, valtext, sizeof(valtext)-1);
dprintf("r: %d\n", r);
if (r < 0) {
printf("error: unable to get channel option\n");
return 1;
}
dprintf("i: %d, text: %s\n", i, valtext);
if (strcmp(valtext,sval)==0) {
dval = i;
found = 1;
break;
}
}
dprintf("found: %d\n", found);
if (!found) {
printf("error: option not found\n");
return 1;
}
} else {
dval = atof(sval);
dprintf("cval: %f, dval: %f\n", cval, dval);
if (dval == cval) return 0;
}
r = SetChannelValue(chan,handle,dval);
dprintf("r: %d\n", r);
if (r < 0) {
printf("error: unable to set value: %f\n",dval);
return 1;
}
return 0;
}
static int getmyhomedir(char *dest, int destlen) {
FILE *fp;
char line[256],*p,*s;
long uid;
int len,id,i;
// struct passwd *pw;
uid = getuid();
fp = fopen("/etc/passwd","r");
if (!fp) {
perror("fopen /etc/passwd");
return 1;
}
while(fgets(line,sizeof(line)-1,fp)) {
len = strlen(line);
if (!len) continue;
while(len > 1 && isspace(line[len-1])) len--;
dprintf("line[%d]: %d\n", len, line[len]);
line[len] = 0;
dprintf("line: %s\n", line);
p = line;
for(i=0; i < 2; i++) {
p = strchr(p+1,':');
if (!p) goto getmyhomedir_error;
dprintf("p(%d): %s\n", i, p);
}
s = p+1;
p = strchr(s,':');
if (p) *p = 0;
dprintf("s: %s\n", s);
id = atoi(s);
if (id == uid) {
for(i=0; i < 2; i++) {
p = strchr(p+1,':');
if (!p) goto getmyhomedir_error;
dprintf("p(%d): %s\n", i, p);
}
s = p+1;
p = strchr(s,':');
if (p) *p = 0;
dprintf("s: %s\n", s);
dest[0] = 0;
strncat(dest,s,destlen);
len = strlen(dest);
if (dest[len] != '/') strcat(dest,"/");
fclose(fp);
return 0;
}
}
getmyhomedir_error:
fclose(fp);
return 1;
}
char *find_config_file(char *name) {
static char temp[1024];
if (access(name,R_OK)==0) return name;
if (!getmyhomedir(temp,sizeof(temp)-1)) {
strcat(temp,"etc/");
strcat(temp,name);
dprintf("temp: %s\n", temp);
if (access(temp,R_OK)==0) return temp;
}
sprintf(temp,"/etc/%s",name);
if (access(temp,R_OK)==0) return temp;
sprintf(temp,"/usr/local/etc/%s",name);
if (access(temp,R_OK)==0) return temp;
sprintf(temp,"/opt/mybmm/etc/%s",name);
if (access(temp,R_OK)==0) return temp;
return 0;
}
void usage(char *myname) {
printf("usage: %s [-alps] -n chan | ChanName [value]\n",myname);
return;
}
int main(int argc, char **argv) {
int opt,all,list,param,action,type;
int i,driverCount,found,r,done;
char *progName,*configFile,name[32],device_type[32];
DWORD drivers[5],devCount,count,deviceHandles[1];
progName = argv[0];
all = list = param = 0;
while ((opt=getopt(argc, argv, "alph")) != -1) {
switch (opt) {
case 'a':
all = 1;
break;
case 'l':
list = 1;
break;
case 'p':
param = 1;
break;
default:
case 'h':
usage(progName);
exit(0);
break;
}
}
dprintf("all: %d, list: %d, param: %d\n", all, list, param);
if (all && !list) printf("info: all flag (-a) only applies to list (-l), ignored.\n");
argc -= optind;
argv += optind;
for(i=0; i < argc; i++) dprintf("arg[%d]: %s\n",i,argv[i]);
dprintf("argc: %d\n", argc);
if (argc < 1 && list == 0) {
usage(progName);
return 1;
}
if (list) {
action = 0;
if (all) {
type = CHANTYPE_ALL;
} else if (param) {
type = CHANTYPE_PARM;
} else {
type = CHANTYPE_SPOT;
}
} else if (argc < 2) {
action = 1;
} else {
action = 2;
}
configFile = find_config_file("yasdi.ini");
if (!configFile) {
printf("error: unable to find yasdi.ini\n");
return 1;
}
dprintf("configFile: %s\n", configFile);
yasdiMasterInitialize(configFile,&count);
driverCount = yasdiMasterGetDriver(drivers, 5);
dprintf("driverCount: %d\n",driverCount);
if (driverCount < 0 || driverCount > 5) {
printf("error initializing drivers\n");
return 1;
}
found=0;
for(i=0; i < count; i++) {
yasdiGetDriverName(drivers[i], name, sizeof(name));
if (yasdiSetDriverOnline(drivers[i])) {
dprintf("Driver: %s: Online\n", name);
found=1;
} else {
printf("Driver: %s: FAILED\n",name);
}
}
if (!found) {
printf("error: no drivers online\n");
return 1;
}
done = 0;
while(!done) {
r = DoStartDeviceDetection(1, TRUE);
dprintf("r: %d\n", r);
switch(r) {
case YE_OK:
done = 1;
break;
case YE_NOT_ALL_DEVS_FOUND:
sleep(1);
break;
case YE_DEV_DETECT_IN_PROGRESS:
printf("error: another detection is in progress\n");
goto error;
break;
default:
printf("error: %d\n", r);
goto error;
break;
}
}
TSecurity_SetNewAccessLevel(0,0);
devCount = GetDeviceHandles(deviceHandles, 1);
dprintf("deviceHandles[0]: %d\n", deviceHandles[0]);
for(i=0; i < devCount; i++) {
GetDeviceType(deviceHandles[i], device_type, sizeof(device_type)-1);
dprintf("%d: device_type: %s\n", i, device_type);
}
dprintf("action: %d\n", action);
switch(action) {
case 0:
r = dolist(deviceHandles[0], type, (argc ? argv[0] : 0));
break;
case 1:
r = getparm(deviceHandles[0], argv[0]);
break;
case 2:
r = setparm(deviceHandles[0], argv[0], argv[1]);
break;
}
error:
for(i=0; i < count; i++) {
yasdiGetDriverName(drivers[i], name, sizeof(name));
yasdiSetDriverOffline(drivers[i]);
dprintf("Driver: %s: Offline\n", name);
}
yasdiMasterShutdown();
return r;
}