e0d303575267dd6de122624e3d9fd0a3e762bf48
[collection4.git] / fcgi_test.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <errno.h>
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <dirent.h>
11
12 #include <fcgiapp.h>
13 #include <fcgi_stdio.h>
14
15 #include "common.h"
16 #include "graph_list.h"
17 #include "utils_params.h"
18
19 struct str_array_s
20 {
21   char **ptr;
22   size_t size;
23 };
24 typedef struct str_array_s str_array_t;
25
26 static str_array_t *array_alloc (void) /* {{{ */
27 {
28   str_array_t *a;
29
30   a = malloc (sizeof (*a));
31   if (a == NULL)
32     return (NULL);
33
34   memset (a, 0, sizeof (*a));
35   a->ptr = NULL;
36   a->size = 0;
37
38   return (a);
39 } /* }}} str_array_t *array_alloc */
40
41 static void array_free (str_array_t *a) /* {{{ */
42 {
43   if (a == NULL)
44     return;
45
46   free (a->ptr);
47   a->ptr = NULL;
48   a->size = 0;
49
50   free (a);
51 } /* }}} void array_free */
52
53 static int array_add (const char *entry, void *user_data) /* {{{ */
54 {
55   str_array_t *a = user_data;
56   char **ptr;
57
58   if ((entry == NULL) || (a == NULL))
59     return (EINVAL);
60
61   ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
62   if (ptr == NULL)
63     return (ENOMEM);
64   a->ptr = ptr;
65   ptr = a->ptr + a->size;
66
67   *ptr = strdup (entry);
68   if (*ptr == NULL)
69     return (ENOMEM);
70
71   a->size++;
72   return (0);
73 } /* }}} int array_add */
74
75 static int print_graph (const graph_list_t *gl, void *user_data)
76 {
77   if (gl == NULL)
78     return (EINVAL);
79
80   printf ("host = %s; plugin = %s;", gl->host, gl->plugin);
81   if (gl->plugin_instance != NULL)
82     printf (" plugin_instance = %s;", gl->plugin_instance);
83   printf (" type = %s;", gl->type);
84   if (gl->type_instance != NULL)
85     printf (" type_instance = %s;", gl->type_instance);
86   printf ("\n");
87
88   return (0);
89 } /* }}} int print_graph */
90
91 static int get_graphs_list (char ***ret_graphs, /* {{{ */
92     size_t *ret_graphs_num)
93 {
94   gl_update ();
95   gl_foreach (print_graph, /* user_data = */ NULL);
96
97   return (0);
98 } /* }}} int get_graphs_list */
99
100 static int action_hello (void) /* {{{ */
101 {
102   printf ("Content-Type: text/plain\n\n");
103
104   get_graphs_list (NULL, NULL);
105
106   return (0);
107 } /* }}} int action_hello */
108
109 static int action_usage (void) /* {{{ */
110 {
111   printf ("Content-Type: text/plain\n\n");
112
113   fputs ("Usage:\n"
114       "\n"
115       "  Available actions:\n"
116       "\n"
117       "    * hello\n"
118       "\n", stdout);
119
120   return (0);
121 } /* }}} int action_usage */
122
123 static int handle_request (void) /* {{{ */
124 {
125   const char *action;
126
127   param_init ();
128
129   action = param ("action");
130   if (action == NULL)
131   {
132     return (action_usage ());
133   }
134   else if (strcmp ("hello", action) == 0)
135   {
136     return (action_hello ());
137   }
138   else
139   {
140     return (action_usage ());
141   }
142 } /* }}} int handle_request */
143
144 static int run (void) /* {{{ */
145 {
146   while (FCGI_Accept() >= 0)
147   {
148     handle_request ();
149     param_finish ();
150   }
151
152   return (0);
153 } /* }}} int run */
154
155 int main (int argc, char **argv) /* {{{ */
156 {
157   int status;
158
159   argc = 0;
160   argv = NULL;
161
162   if (FCGX_IsCGI ())
163     status = handle_request ();
164   else
165     status = run ();
166
167   exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
168 } /* }}} int main */
169
170 /* vim: set sw=2 sts=2 et fdm=marker : */