valgrind: stop openssl still reachable complaints
[platform/upstream/libwebsockets.git] / plugins / protocol_lws_server_status.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * The test apps are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20
21 #define LWS_DLL
22 #define LWS_INTERNAL
23 #include "../lib/libwebsockets.h"
24 #include <string.h>
25 #include <stdlib.h>
26
27 struct lws_ss_load_sample {
28         time_t t;
29         int load_x100;
30 };
31
32 struct lws_ss_filepath {
33         struct lws_ss_filepath *next;
34         char filepath[128];
35 };
36
37 struct lws_ss_dumps {
38         char buf[32768];
39         int length;
40
41         struct lws_ss_load_sample load[64];
42         int load_head;
43         int load_tail;
44 };
45
46 struct per_session_data__server_status {
47         int ver;
48         int pos;
49 };
50
51 struct per_vhost_data__lws_server_status {
52         uv_timer_t timeout_watcher;
53         struct lws_context *context;
54         int hide_vhosts;
55         int tow_flag;
56         struct lws_ss_dumps d;
57         struct lws_ss_filepath *fp;
58 };
59
60 static const struct lws_protocols protocols[1];
61
62 static void
63 uv_timeout_cb_server_status(uv_timer_t *w
64 #if UV_VERSION_MAJOR == 0
65                 , int status
66 #endif
67 )
68 {
69         struct per_vhost_data__lws_server_status *v = lws_container_of(w,
70                         struct per_vhost_data__lws_server_status,
71                         timeout_watcher);
72         struct lws_ss_filepath *fp;
73         char *p = v->d.buf + LWS_PRE, contents[256], pure[256];
74         int n, l, first = 1, fd;
75
76         l = sizeof(v->d.buf) - LWS_PRE - 1;
77
78         n = lws_snprintf(p, l, "{\"i\":");
79         p += n;
80         l -= n;
81
82         n = lws_json_dump_context(v->context, p, l, v->hide_vhosts);
83         p += n;
84         l -= n;
85
86         n = lws_snprintf(p, l, ", \"files\": [");
87         p += n;
88         l -= n;
89
90         fp = v->fp;
91         while (fp) {
92                 if (!first) {
93                         n = lws_snprintf(p, l, ",");
94                         p += n;
95                         l -= n;
96                 }
97                 fd = open(fp->filepath, LWS_O_RDONLY);
98                 if (fd >= 0) {
99                         n = read(fd, contents, sizeof(contents) - 1);
100                         if (n >= 0) {
101                                 contents[n] = '\0';
102                                 lws_json_purify(pure, contents, sizeof(pure));
103
104                                 n = lws_snprintf(p, l, "{\"path\":\"%s\",\"val\":\"%s\"}",
105                                                  fp->filepath, pure);
106                                 p += n;
107                                 l -= n;
108                                 first = 0;
109                         }
110                         close(fd);
111                 }
112                 fp = fp->next;
113         }
114         n = lws_snprintf(p, l, "]}");
115         p += n;
116         l -= n;
117
118         v->d.length = p - (v->d.buf + LWS_PRE);
119
120         lws_callback_on_writable_all_protocol(v->context, &protocols[0]);
121 }
122
123 static int
124 callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
125                            void *user, void *in, size_t len)
126 {
127         const struct lws_protocol_vhost_options *pvo =
128                         (const struct lws_protocol_vhost_options *)in;
129         struct per_vhost_data__lws_server_status *v =
130                         (struct per_vhost_data__lws_server_status *)
131                         lws_protocol_vh_priv_get(lws_get_vhost(wsi),
132                                         lws_get_protocol(wsi));
133         struct lws_ss_filepath *fp, *fp1, **fp_old;
134         int m, period = 1000;
135
136         switch (reason) {
137
138         case LWS_CALLBACK_ESTABLISHED:
139                 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
140                 lws_callback_on_writable(wsi);
141                 break;
142
143         case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
144                 if (v)
145                         break;
146
147                 lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
148                                 lws_get_protocol(wsi),
149                                 sizeof(struct per_vhost_data__lws_server_status));
150                 v = (struct per_vhost_data__lws_server_status *)
151                                 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
152                                 lws_get_protocol(wsi));
153
154                 fp_old = &v->fp;
155
156                 while (pvo) {
157                         if (!strcmp(pvo->name, "hide-vhosts"))
158                                 v->hide_vhosts = atoi(pvo->value);
159                         if (!strcmp(pvo->name, "update-ms"))
160                                 period = atoi(pvo->value);
161                         if (!strcmp(pvo->name, "filepath")) {
162                                 fp = malloc(sizeof(*fp));
163                                 fp->next = NULL;
164                                 lws_snprintf(&fp->filepath[0], sizeof(fp->filepath), "%s", pvo->value);
165                                 *fp_old = fp;
166                                 fp_old = &fp->next;
167                         }
168                         pvo = pvo->next;
169                 }
170                 v->context = lws_get_context(wsi);
171                 uv_timer_init(lws_uv_getloop(v->context, 0), &v->timeout_watcher);
172                 uv_timer_start(&v->timeout_watcher,
173                                 uv_timeout_cb_server_status, 2000, period);
174                 break;
175
176         case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
177         //      lwsl_notice("ss: LWS_CALLBACK_PROTOCOL_DESTROY: v=%p, ctx=%p\n", v, v->context);
178                 if (!v)
179                         break;
180                 uv_timer_stop(&v->timeout_watcher);
181                 uv_close((uv_handle_t *)&v->timeout_watcher, NULL);
182                 fp = v->fp;
183                 while (fp) {
184                         fp1= fp->next;
185                         free(fp);
186                         fp = fp1;
187                 }
188                 break;
189
190         case LWS_CALLBACK_SERVER_WRITEABLE:
191                 m = lws_write(wsi, (unsigned char *)v->d.buf + LWS_PRE,
192                               v->d.length, LWS_WRITE_TEXT);
193                 if (m < 0)
194                         return -1;
195                 break;
196
197         default:
198                 break;
199         }
200
201         return 0;
202 }
203
204 static const struct lws_protocols protocols[] = {
205         {
206                 "lws-server-status",
207                 callback_lws_server_status,
208                 sizeof(struct per_session_data__server_status),
209                 1024,
210         },
211 };
212
213 LWS_EXTERN LWS_VISIBLE int
214 init_protocol_lws_server_status(struct lws_context *context,
215                              struct lws_plugin_capability *c)
216 {
217         if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
218                 lwsl_err("Plugin API %d, library API %d",
219                          LWS_PLUGIN_API_MAGIC, c->api_magic);
220                 return 1;
221         }
222
223         c->protocols = protocols;
224         c->count_protocols = ARRAY_SIZE(protocols);
225         c->extensions = NULL;
226         c->count_extensions = 0;
227
228         return 0;
229 }
230
231 LWS_EXTERN LWS_VISIBLE int
232 destroy_protocol_lws_server_status(struct lws_context *context)
233 {
234         return 0;
235 }
236