File: extensions.c

package info (click to toggle)
bandwidthd 2.0.1+cvs20090917-10
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,204 kB
  • sloc: sh: 3,008; ansic: 2,746; php: 1,263; yacc: 216; makefile: 162; lex: 36; sql: 27
file content (181 lines) | stat: -rw-r--r-- 3,467 bytes parent folder | download | duplicates (6)
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
#include "bandwidthd.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

extern struct config config;

static char *execute_extension(char * filename);

struct extensions *execute_extensions(void)
	{
    struct dirent **namelist;
    int Counter, n;
	struct stat statbuffer;
	char *filename;	
	struct extensions *results = NULL;
	struct extensions **resptr;
	char *ptr;

	resptr = &results;

    n = scandir(EXTENSION_DIR, &namelist, 0, alphasort);
    if (n < 0)
		syslog(LOG_ERR, "Failed to open %s for executing extensions", EXTENSION_DIR);
    else 
		{
    	for(Counter = 0; Counter < n; Counter++) 
			{
			filename = malloc(strlen(EXTENSION_DIR)+strlen(namelist[Counter]->d_name)+2);
			sprintf(filename, "%s/%s", EXTENSION_DIR, namelist[Counter]->d_name);
			stat(filename, &statbuffer);
			
			if (S_ISREG(statbuffer.st_mode))
				{
				ptr = execute_extension(filename);
				if (ptr)
					{
					*resptr = malloc(sizeof(struct extensions));
					(*resptr)->name = strdup(namelist[Counter]->d_name);
					(*resptr)->value = ptr;			
					(*resptr)->next = NULL;
					resptr = &((*resptr)->next);
					}
				}
			free(filename);
            free(namelist[Counter]);
            }
        free(namelist);
        }

	return(results);
	}

static char *execute_extension(char *filename)
	{
	int ExtPipe[2];
	pid_t extpid;
	char buffer[1500];
	char *res = NULL;
	char *ptr;
	ssize_t len;
	int child;
	int Counter;

	fd_set rset;
	fd_set eset;

    if (pipe(ExtPipe))
        {
        syslog(LOG_ERR, "Failed to open pipe for extension processing");
        return(NULL);
        }

	if (!(extpid = fork()))
		{
		close(ExtPipe[0]);
		dup2(ExtPipe[1], 1);

        execl(filename, filename, config.dev, NULL);
        syslog(LOG_ERR, "Failed to launch extension %s", filename);
        _exit(1);
		}

	close(ExtPipe[1]);
	
	child = 1;
	while (child)
		{
		child = !waitpid(extpid, NULL, WNOHANG);
	
		// If child is running drop into a select and wait for data
		if (child)
			{
			FD_ZERO(&rset);
			FD_SET(ExtPipe[0], &rset);
			FD_ZERO(&eset);
			FD_SET(ExtPipe[0], &eset);
			
			select(ExtPipe[0]+1, &rset, NULL, &eset, NULL);
			}
		
		len = 1;
		while (len)
			{
			len = read(ExtPipe[0], buffer, 1499);  
			buffer[len] = '\0';

			if (res)
				{
				ptr = malloc(strlen(res)+len+1);
				sprintf(ptr, "%s%s", res, buffer);
				free(res);
				}
			else
				{
				ptr = malloc(len+1);
				sprintf(ptr, "%s", buffer);
				}
			res = ptr;
			}
		}
	close(ExtPipe[0]);

	// Trim whitespace from end
	for(Counter = strlen(res)-1; Counter >= 0; Counter--)
		{
		if (res[Counter] == ' ' || res[Counter] == '\t' || res[Counter] == '\r' || res[Counter] == '\n')
			res[Counter] = '\0';
		else
			break;
		}

	// Filter empty responses (Also filters all whitespace responses because of previouse trim)
	if (strlen(res) < 1)
		{
		free(res);
		return(NULL);
		}
	else
		return(res);
	}

void destroy_extension_data(struct extensions *ext)
	{
	struct extensions *ptr;
	struct extensions *ptr2;
	ptr = ext;
	while(ptr)
		{
		if (ptr->name) 
			free(ptr->name);
		if (ptr->value)
			free(ptr->value);
		ptr2 = ptr->next;
		free(ptr);
		ptr = ptr2;
		}
	}

/*
int main(void)
	{
	struct extensions *res;
	struct extensions *resptr;

	res = execute_extensions();
	resptr = res;

	while(resptr)
		{
		printf("%s\n", resptr->name);
		printf("%s\n", resptr->value);
		resptr = resptr->next;
		}

	destroy_extensions(res);
	return(0);
	}

*/