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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
|
/*
* Stonith module for EXTERNAL Stonith device
*
* Copyright (c) 2001 SuSE Linux AG
* Portions Copyright (c) 2004, tummy.com, ltd.
*
* Based on ssh.c, Authors: Joachim Gleissner <jg@suse.de>,
* Lars Marowsky-Bree <lmb@suse.de>
* Modified for external.c: Scott Kleihege <scott@tummy.com>
* Reviewed, tested, and config parsing: Sean Reifschneider <jafo@tummy.com>
* And overhauled by Lars Marowsky-Bree <lmb@suse.de>, so the circle
* closes...
* Mangled by Zhaokai <zhaokai@cn.ibm.com>, IBM, 2005
* Changed to allow full-featured external plugins by Dave Blaschke
* <debltc@us.ibm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <lha_internal.h>
#include <dirent.h>
#include "stonith_plugin_common.h"
#define PIL_PLUGIN external
#define PIL_PLUGIN_S "external"
#define PIL_PLUGINLICENSE LICENSE_LGPL
#define PIL_PLUGINLICENSEURL URL_LGPL
#include <pils/plugin.h>
static StonithPlugin * external_new(const char *);
static void external_destroy(StonithPlugin *);
static int external_set_config(StonithPlugin *, StonithNVpair *);
static const char * const * external_get_confignames(StonithPlugin *);
static const char * external_getinfo(StonithPlugin * s, int InfoType);
static int external_status(StonithPlugin * );
static int external_reset_req(StonithPlugin * s, int request, const char * host);
static char ** external_hostlist(StonithPlugin *);
static struct stonith_ops externalOps ={
external_new, /* Create new STONITH object */
external_destroy, /* Destroy STONITH object */
external_getinfo, /* Return STONITH info string */
external_get_confignames, /* Return STONITH info string */
external_set_config, /* Get configuration from NVpairs */
external_status, /* Return STONITH device status */
external_reset_req, /* Request a reset */
external_hostlist, /* Return list of supported hosts */
};
PIL_PLUGIN_BOILERPLATE2("1.0", Debug)
static const PILPluginImports* PluginImports;
static PILPlugin* OurPlugin;
static PILInterface* OurInterface;
static StonithImports* OurImports;
static void* interfprivate;
PIL_rc
PIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports);
PIL_rc
PIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports)
{
/* Force the compiler to do a little type checking */
(void)(PILPluginInitFun)PIL_PLUGIN_INIT;
PluginImports = imports;
OurPlugin = us;
/* Register ourself as a plugin */
imports->register_plugin(us, &OurPIExports);
/* Register our interface implementation */
return imports->register_interface(us, PIL_PLUGINTYPE_S
, PIL_PLUGIN_S
, &externalOps
, NULL /*close */
, &OurInterface
, (void*)&OurImports
, &interfprivate);
}
/*
* EXTERNAL STONITH device
*/
struct pluginDevice {
StonithPlugin sp;
const char * pluginid;
GHashTable * cmd_opts;
char * subplugin;
char ** confignames;
char * outputbuf;
};
static const char * pluginid = "ExternalDevice-Stonith";
static const char * NOTpluginID = "External device has been destroyed";
/* Prototypes */
/* Run the command with op and return the exit status + the output
* (NULL -> discard output) */
static int external_run_cmd(struct pluginDevice *sd, const char *op,
char **output);
/* Just free up the configuration and the memory, if any */
static void external_unconfig(struct pluginDevice *sd);
static int
external_status(StonithPlugin *s)
{
struct pluginDevice * sd;
const char * op = "status";
int rc;
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
ERRIFWRONGDEV(s,S_OOPS);
sd = (struct pluginDevice*) s;
if (sd->subplugin == NULL) {
LOG(PIL_CRIT, "%s: invoked without subplugin", __FUNCTION__);
return(S_OOPS);
}
rc = external_run_cmd(sd, op, NULL);
if (rc != 0) {
LOG(PIL_WARN, "%s: '%s %s' failed with rc %d",
__FUNCTION__, sd->subplugin, op, rc);
}
else {
if (Debug) {
LOG(PIL_DEBUG, "%s: running '%s %s' returned %d",
__FUNCTION__, sd->subplugin, op, rc);
}
}
return rc;
}
static int
get_num_tokens(char *str)
{
int namecount = 0;
while (*str != EOS) {
str += strspn(str, WHITESPACE);
if (*str == EOS)
break;
str += strcspn(str, WHITESPACE);
namecount++;
}
return namecount;
}
static char **
external_hostlist(StonithPlugin *s)
{
struct pluginDevice* sd;
const char * op = "gethosts";
int rc, i, namecount;
char ** ret;
char * output = NULL;
char * tmp;
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
ERRIFNOTCONFIGED(s,NULL);
sd = (struct pluginDevice*) s;
if (sd->subplugin == NULL) {
LOG(PIL_CRIT, "%s: invoked without subplugin", __FUNCTION__);
return(NULL);
}
rc = external_run_cmd(sd, op, &output);
if (rc != 0) {
LOG(PIL_CRIT, "%s: '%s %s' failed with rc %d",
__FUNCTION__, sd->subplugin, op, rc);
if (output) {
LOG(PIL_CRIT, "plugin output: %s", output);
FREE(output);
}
return NULL;
}
if (Debug) {
LOG(PIL_DEBUG, "%s: running '%s %s' returned %d",
__FUNCTION__, sd->subplugin, op, rc);
}
if (!output) {
LOG(PIL_CRIT, "%s: '%s %s' returned an empty hostlist",
__FUNCTION__, sd->subplugin, op);
return NULL;
}
namecount = get_num_tokens(output);
ret = MALLOC((namecount+1)*sizeof(char *));
if (!ret) {
LOG(PIL_CRIT, "%s: out of memory", __FUNCTION__);
FREE(output);
return NULL;
}
memset(ret, 0, (namecount+1)*sizeof(char *));
/* White-space split the output here */
i = 0;
tmp = strtok(output, WHITESPACE);
while (tmp != NULL) {
if (Debug) {
LOG(PIL_DEBUG, "%s: %s host %s",
__FUNCTION__, sd->subplugin, tmp);
}
ret[i] = STRDUP(tmp);
if (!ret[i]) {
LOG(PIL_CRIT, "%s: out of memory", __FUNCTION__);
FREE(output);
stonith_free_hostlist(ret);
return NULL;
}
i++;
tmp = strtok(NULL, WHITESPACE);
}
FREE(output);
if (i == 0) {
LOG(PIL_CRIT, "%s: '%s %s' returned an empty hostlist",
__FUNCTION__, sd->subplugin, op);
stonith_free_hostlist(ret);
ret = NULL;
}
return(ret);
}
static int
external_reset_req(StonithPlugin * s, int request, const char * host)
{
struct pluginDevice * sd;
const char * op;
int rc;
char * args1and2;
int argslen;
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
ERRIFNOTCONFIGED(s,S_OOPS);
if (Debug) {
LOG(PIL_DEBUG, "Host external-reset initiating on %s", host);
}
sd = (struct pluginDevice*) s;
if (sd->subplugin == NULL) {
LOG(PIL_CRIT, "%s: invoked without subplugin", __FUNCTION__);
return(S_OOPS);
}
switch (request) {
case ST_GENERIC_RESET:
op = "reset";
break;
case ST_POWEROFF:
op = "off";
break;
case ST_POWERON:
op = "on";
break;
default:
LOG(PIL_CRIT, "%s: Unknown stonith request %d",
__FUNCTION__, request);
return S_OOPS;
break;
}
argslen = strlen(op) + strlen(host) + 2 /* 1 for blank, 1 for EOS */;
args1and2 = (char *)MALLOC(argslen);
if (args1and2 == NULL) {
LOG(PIL_CRIT, "%s: out of memory!", __FUNCTION__);
return S_OOPS;
}
rc = snprintf(args1and2, argslen, "%s %s", op, host);
if (rc <= 0 || rc >= argslen) {
FREE(args1and2);
return S_OOPS;
}
rc = external_run_cmd(sd, args1and2, NULL);
FREE(args1and2);
if (rc != 0) {
LOG(PIL_CRIT, "%s: '%s %s' for host %s failed with rc %d",
__FUNCTION__, sd->subplugin, op, host, rc);
return S_RESETFAIL;
}
else {
if (Debug) {
LOG(PIL_DEBUG, "%s: running '%s %s' returned %d",
__FUNCTION__, sd->subplugin, op, rc);
}
return S_OK;
}
}
static int
external_parse_config_info(struct pluginDevice* sd, StonithNVpair * info)
{
char * key;
char * value;
StonithNVpair * nv;
sd->cmd_opts = g_hash_table_new(g_str_hash, g_str_equal);
/* TODO: Maybe treat "" as delimeters too so
* whitespace can be passed to the plugins... */
for (nv = info; nv->s_name; nv++) {
if (!nv->s_name || !nv->s_value) {
continue;
}
key = STRDUP(nv->s_name);
if (!key) {
goto err_mem;
}
value = STRDUP(nv->s_value);
if (!value) {
FREE(key);
goto err_mem;
}
g_hash_table_insert(sd->cmd_opts, key, value);
}
return(S_OK);
err_mem:
LOG(PIL_CRIT, "%s: out of memory!", __FUNCTION__);
external_unconfig(sd);
return(S_OOPS);
}
static gboolean
let_remove_eachitem(gpointer key, gpointer value, gpointer user_data)
{
if (key) {
FREE(key);
}
if (value) {
FREE(value);
}
return TRUE;
}
static void
external_unconfig(struct pluginDevice *sd) {
if (sd->cmd_opts) {
g_hash_table_foreach_remove(sd->cmd_opts,
let_remove_eachitem, NULL);
g_hash_table_destroy(sd->cmd_opts);
sd->cmd_opts = NULL;
}
}
/*
* Parse the information in the given string
* and stash it away...
*/
static int
external_set_config(StonithPlugin* s, StonithNVpair *list)
{
struct pluginDevice * sd;
char ** p;
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
ERRIFWRONGDEV(s,S_OOPS);
/* make sure that command has not already been set */
if (s->isconfigured) {
return(S_OOPS);
}
sd = (struct pluginDevice*) s;
if (sd->subplugin == NULL) {
LOG(PIL_CRIT, "%s: invoked without subplugin", __FUNCTION__);
return(S_OOPS);
}
if (sd->confignames == NULL) {
/* specified by name=value pairs, check required parms */
if (external_get_confignames(s) == NULL) {
return(S_OOPS);
}
for (p = sd->confignames; *p; p++) {
if (OurImports->GetValue(list, *p) == NULL) {
LOG(PIL_DEBUG, "Cannot get parameter %s from "
"StonithNVpair", *p);
}
}
}
return external_parse_config_info(sd, list);
}
/* Only interested in regular files that are also executable */
static int
exec_select(const struct dirent *dire)
{
struct stat statf;
char filename[FILENAME_MAX];
int rc;
rc = snprintf(filename, FILENAME_MAX, "%s/%s",
STONITH_EXT_PLUGINDIR, dire->d_name);
if (rc <= 0 || rc >= FILENAME_MAX) {
return 0;
}
if ((stat(filename, &statf) == 0) &&
(S_ISREG(statf.st_mode)) &&
(statf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))) {
if (statf.st_mode & (S_IWGRP|S_IWOTH)) {
LOG(PIL_WARN, "Executable file %s ignored "
"(writable by group/others)", filename);
return 0;
}else{
return 1;
}
}
return 0;
}
/*
* Return STONITH config vars
*/
static const char * const *
external_get_confignames(StonithPlugin* p)
{
struct pluginDevice * sd;
const char * op = "getconfignames";
int i, rc;
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
sd = (struct pluginDevice *)p;
if (sd->subplugin != NULL) {
/* return list of subplugin's required parameters */
char *output = NULL, *pch;
int namecount;
rc = external_run_cmd(sd, op, &output);
if (rc != 0) {
LOG(PIL_CRIT, "%s: '%s %s' failed with rc %d",
__FUNCTION__, sd->subplugin, op, rc);
if (output) {
LOG(PIL_CRIT, "plugin output: %s", output);
FREE(output);
}
return NULL;
}
if (Debug) {
LOG(PIL_DEBUG, "%s: '%s %s' returned %d",
__FUNCTION__, sd->subplugin, op, rc);
if (output) {
LOG(PIL_DEBUG, "plugin output: %s", output);
}
}
namecount = get_num_tokens(output);
sd->confignames = (char **)MALLOC((namecount+1)*sizeof(char *));
if (sd->confignames == NULL) {
LOG(PIL_CRIT, "%s: out of memory", __FUNCTION__);
if (output) { FREE(output); }
return NULL;
}
/* now copy over confignames */
pch = strtok(output, WHITESPACE);
for (i = 0; i < namecount; i++) {
if (Debug) {
LOG(PIL_DEBUG, "%s: %s configname %s",
__FUNCTION__, sd->subplugin, pch);
}
sd->confignames[i] = STRDUP(pch);
pch = strtok(NULL, WHITESPACE);
}
FREE(output);
sd->confignames[namecount] = NULL;
}else{
/* return list of subplugins in external directory */
struct dirent ** files = NULL;
int dircount;
/* get the external plugin's confignames (list of subplugins) */
dircount = scandir(STONITH_EXT_PLUGINDIR, &files,
SCANSEL_CAST exec_select, NULL);
if (dircount < 0) {
return NULL;
}
sd->confignames = (char **)MALLOC((dircount+1)*sizeof(char *));
if (!sd->confignames) {
LOG(PIL_CRIT, "%s: out of memory", __FUNCTION__);
return NULL;
}
for (i = 0; i < dircount; i++) {
sd->confignames[i] = STRDUP(files[i]->d_name);
free(files[i]);
files[i] = NULL;
}
free(files);
sd->confignames[dircount] = NULL;
}
return (const char * const *)sd->confignames;
}
/*
* Return STONITH info string
*/
static const char *
external_getinfo(StonithPlugin * s, int reqtype)
{
struct pluginDevice* sd;
char * output = NULL;
const char * op;
int rc;
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
ERRIFWRONGDEV(s,NULL);
sd = (struct pluginDevice *)s;
if (sd->subplugin == NULL) {
LOG(PIL_CRIT, "%s: invoked without subplugin", __FUNCTION__);
return(NULL);
}
switch (reqtype) {
case ST_DEVICEID:
op = "getinfo-devid";
break;
case ST_DEVICENAME:
op = "getinfo-devname";
break;
case ST_DEVICEDESCR:
op = "getinfo-devdescr";
break;
case ST_DEVICEURL:
op = "getinfo-devurl";
break;
case ST_CONF_XML:
op = "getinfo-xml";
break;
default:
return NULL;
}
rc = external_run_cmd(sd, op, &output);
if (rc != 0) {
LOG(PIL_CRIT, "%s: '%s %s' failed with rc %d",
__FUNCTION__, sd->subplugin, op, rc);
if (output) {
LOG(PIL_CRIT, "plugin output: %s", output);
FREE(output);
}
}
else {
if (Debug) {
LOG(PIL_DEBUG, "%s: '%s %s' returned %d",
__FUNCTION__, sd->subplugin, op, rc);
}
if (sd->outputbuf != NULL) {
FREE(sd->outputbuf);
}
sd->outputbuf = output;
return(output);
}
return(NULL);
}
/*
* EXTERNAL Stonith destructor...
*/
static void
external_destroy(StonithPlugin *s)
{
struct pluginDevice * sd;
char ** p;
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
VOIDERRIFWRONGDEV(s);
sd = (struct pluginDevice *)s;
sd->pluginid = NOTpluginID;
external_unconfig(sd);
if (sd->confignames != NULL) {
for (p = sd->confignames; *p; p++) {
FREE(*p);
}
FREE(sd->confignames);
sd->confignames = NULL;
}
if (sd->subplugin != NULL) {
FREE(sd->subplugin);
sd->subplugin = NULL;
}
if (sd->outputbuf != NULL) {
FREE(sd->outputbuf);
sd->outputbuf = NULL;
}
FREE(sd);
}
/* Create a new external Stonith device */
static StonithPlugin *
external_new(const char *subplugin)
{
struct pluginDevice* sd = ST_MALLOCT(struct pluginDevice);
if (Debug) {
LOG(PIL_DEBUG, "%s: called.", __FUNCTION__);
}
if (sd == NULL) {
LOG(PIL_CRIT, "out of memory");
return(NULL);
}
memset(sd, 0, sizeof(*sd));
sd->pluginid = pluginid;
if (subplugin != NULL) {
sd->subplugin = STRDUP(subplugin);
if (sd->subplugin == NULL) {
FREE(sd);
return(NULL);
}
}
sd->sp.s_ops = &externalOps;
return &(sd->sp);
}
static void
ext_add_to_env(gpointer key, gpointer value, gpointer user_data)
{
if (setenv((char *)key, (char *)value, 1) != 0) {
LOG(PIL_CRIT, "%s: setenv failed.", __FUNCTION__);
}
}
static void
ext_del_from_env(gpointer key, gpointer value, gpointer user_data)
{
unsetenv((char *)key);
}
#define LOGTAG_VAR "HA_LOGTAG"
/* Run the command with op as command line argument(s) and return the exit
* status + the output */
static int
external_run_cmd(struct pluginDevice *sd, const char *op, char **output)
{
const int BUFF_LEN=4096;
char buff[BUFF_LEN];
int read_len = 0;
int status, rc;
char * data = NULL;
FILE * file;
char cmd[FILENAME_MAX+64];
struct stat buf;
int slen;
char *path, *new_path, *logtag, *savevar = NULL;
int new_path_len, logtag_len;
gboolean nodata;
rc = snprintf(cmd, FILENAME_MAX, "%s/%s",
STONITH_EXT_PLUGINDIR, sd->subplugin);
if (rc <= 0 || rc >= FILENAME_MAX) {
LOG(PIL_CRIT, "%s: external command too long.", __FUNCTION__);
return -1;
}
if (stat(cmd, &buf) != 0) {
LOG(PIL_CRIT, "%s: stat(2) of %s failed: %s",
__FUNCTION__, cmd, strerror(errno));
return -1;
}
if (!S_ISREG(buf.st_mode)
|| (!(buf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))) {
LOG(PIL_CRIT, "%s: %s found NOT to be executable.",
__FUNCTION__, cmd);
return -1;
}
if (buf.st_mode & (S_IWGRP|S_IWOTH)) {
LOG(PIL_CRIT, "%s: %s found to be writable by group/others, "
"NOT executing for security purposes.",
__FUNCTION__, cmd);
return -1;
}
strcat(cmd, " ");
strcat(cmd, op);
/* We only have a global environment to use here. So we add our
* options to it, and then later remove them again. */
if (sd->cmd_opts) {
g_hash_table_foreach(sd->cmd_opts, ext_add_to_env, NULL);
}
/* external plugins need path to ha_log.sh */
path = getenv("PATH");
if (strncmp(GLUE_SHARED_DIR,path,strlen(GLUE_SHARED_DIR))) {
new_path_len = strlen(path)+strlen(GLUE_SHARED_DIR)+2;
new_path = (char *)g_malloc(new_path_len);
snprintf(new_path, new_path_len, "%s:%s", GLUE_SHARED_DIR, path);
setenv("PATH", new_path, 1);
g_free(new_path);
}
/* set the logtag appropriately */
logtag_len = strlen(PIL_PLUGIN_S)+strlen(sd->subplugin)+2;
logtag = (char *)g_malloc(logtag_len);
snprintf(logtag, logtag_len, "%s/%s", PIL_PLUGIN_S, sd->subplugin);
if (getenv(LOGTAG_VAR)) {
savevar = g_strdup(getenv(LOGTAG_VAR));
}
setenv(LOGTAG_VAR, logtag, 1);
g_free(logtag);
if (Debug) {
LOG(PIL_DEBUG, "%s: Calling '%s'", __FUNCTION__, cmd );
}
file = popen(cmd, "r");
if (NULL==file) {
LOG(PIL_CRIT, "%s: Calling '%s' failed",
__FUNCTION__, cmd);
rc = -1;
goto out;
}
if (output) {
slen=0;
data = MALLOC(1);
data[slen] = EOS;
}
while (!feof(file)) {
nodata = TRUE;
if (output) {
read_len = fread(buff, 1, BUFF_LEN, file);
if (read_len > 0) {
data = REALLOC(data, slen+read_len+1);
if (data == NULL) {
break;
}
memcpy(data + slen, buff, read_len);
slen += read_len;
data[slen] = EOS;
nodata = FALSE;
}
} else {
if (fgets(buff, BUFF_LEN, file)) {
LOG(PIL_INFO, "%s: '%s' output: %s", __FUNCTION__, cmd, buff);
nodata = FALSE;
}
}
if (nodata) {
sleep(1);
}
}
if (output && !data) {
LOG(PIL_CRIT, "%s: out of memory", __FUNCTION__);
rc = -1;
goto out;
}
status = pclose(file);
if (WIFEXITED(status)) {
rc = WEXITSTATUS(status);
if (rc != 0 && Debug) {
LOG(PIL_DEBUG,
"%s: Calling '%s' returned %d", __FUNCTION__, cmd, rc);
}
} else {
if (WIFSIGNALED(status)) {
LOG(PIL_CRIT, "%s: '%s' got signal %d",
__FUNCTION__, cmd, WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
LOG(PIL_INFO, "%s: '%s' stopped with signal %d",
__FUNCTION__, cmd, WSTOPSIG(status));
} else {
LOG(PIL_CRIT, "%s: '%s' exited abnormally (core dumped?)",
__FUNCTION__, cmd);
}
rc = -1;
}
if (Debug && output && data) {
LOG(PIL_DEBUG, "%s: '%s' output: %s", __FUNCTION__, cmd, data);
}
out:
if (savevar) {
setenv(LOGTAG_VAR, savevar, 1);
g_free(savevar);
} else {
unsetenv(LOGTAG_VAR);
}
if (sd->cmd_opts) {
g_hash_table_foreach(sd->cmd_opts, ext_del_from_env, NULL);
}
if (!rc) {
if (output) {
*output = data;
}
} else {
if (data) {
FREE(data);
}
if (output) {
*output = NULL;
}
}
return rc;
}
|