add validation check for getenv
[platform/core/uifw/libtdm.git] / src / tdm_server.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm.h"
41 #include "tdm_private.h"
42 #include "tdm_list.h"
43 #include "tdm-server-protocol.h"
44
45 /* CAUTION:
46  * - tdm server doesn't care about thread things.
47  * - DO NOT use the TDM internal functions here.
48  *     However, the internal function which does lock/unlock the mutex of
49  *     private_display in itself can be called.
50  * - DO NOT use the tdm_private_display structure here.
51  * - The callback function things can be called in main thread.
52  */
53
54 struct _tdm_private_server {
55         tdm_private_loop *private_loop;
56         struct list_head output_list;
57         struct list_head wait_list;
58 };
59
60 typedef struct _tdm_server_output_info {
61         struct list_head link;
62         tdm_private_server *private_server;
63         struct wl_resource *resource;
64         tdm_output *output;
65         struct list_head vblank_list;
66         unsigned int watch_output_changes;
67 } tdm_server_output_info;
68
69 typedef struct _tdm_server_vblank_info {
70         struct list_head link;
71         tdm_server_output_info *output_info;
72         struct wl_resource *resource;
73
74         tdm_vblank *vblank;
75 } tdm_server_vblank_info;
76
77 typedef struct _tdm_server_wait_info {
78         struct list_head link;
79         tdm_server_vblank_info *vblank_info;
80
81         unsigned int req_id;
82 } tdm_server_wait_info;
83
84 typedef struct _tdm_server_client_info {
85         struct list_head link;
86         pid_t pid;
87         char name[TDM_NAME_LEN];
88 } tdm_server_client_info;
89
90 static tdm_private_server *keep_private_server;
91 static struct list_head client_list;
92
93 static void destroy_wait(tdm_server_wait_info *wait_info);
94
95 static void
96 _tdm_server_get_process_name(pid_t pid, char *name, unsigned int size)
97 {
98         char proc[TDM_NAME_LEN], pname[TDM_NAME_LEN];
99         FILE *h;
100         size_t len;
101
102         snprintf(name, size, "Unknown");
103
104         snprintf(proc, TDM_NAME_LEN, "/proc/%d/cmdline", pid);
105         h = fopen(proc, "r");
106         if (!h)
107                 return;
108
109         len = fread(pname, sizeof(char), TDM_NAME_LEN, h);
110         if (len == 0) {
111                 char *p = strncpy(pname, "NO NAME", sizeof(pname) - 1);
112                 len = p - pname;
113         }
114         pname[len - 1] = '\0';
115
116         strncpy(name, pname, size - 1);
117         name[size - 1] = '\0';
118
119         fclose(h);
120 }
121
122 static tdm_output*
123 _tdm_server_find_output(tdm_private_server *private_server, const char *name)
124 {
125         tdm_private_loop *private_loop = private_server->private_loop;
126         tdm_output *found = NULL;
127
128         if (!strncasecmp(name, "primary", 7) || !strncasecmp(name, "default", 7))
129                 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
130
131         if (!found) {
132                 int count = 0, i;
133
134                 tdm_display_get_output_count(private_loop->dpy, &count);
135
136                 for (i = 0; i < count; i++) {
137                         tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
138                         tdm_output_conn_status status;
139                         const char *model = NULL;
140                         tdm_error ret;
141
142                         ret = tdm_output_get_conn_status(output, &status);
143                         if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
144                                 continue;
145
146                         ret = tdm_output_get_model_info(output, NULL, &model, NULL);
147                         if (ret || !model)
148                                 continue;
149
150                         if (strncmp(model, name, TDM_NAME_LEN))
151                                 continue;
152
153                         found = output;
154                         break;
155                 }
156         }
157
158         return found;
159 }
160
161 static void
162 _tdm_server_send_done(tdm_server_wait_info *wait_info, tdm_error error,
163                                           unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec)
164 {
165         tdm_server_wait_info *found;
166         tdm_server_vblank_info *vblank_info;
167
168         if (!keep_private_server)
169                 return;
170
171         LIST_FIND_ITEM(wait_info, &keep_private_server->wait_list,
172                                    tdm_server_wait_info, link, found);
173         if (!found) {
174                 TDM_DBG("wait_info(%p) is destroyed", wait_info);
175                 return;
176         }
177
178         if (tdm_debug_module & TDM_DEBUG_VBLANK)
179                 TDM_DBG("req_id(%d) done", wait_info->req_id);
180
181         TDM_TRACE_COUNT(ServerDoneVBlank, wait_info->req_id);
182
183         vblank_info = wait_info->vblank_info;
184         wl_tdm_vblank_send_done(vblank_info->resource, wait_info->req_id,
185                                                         sequence, tv_sec, tv_usec, error);
186         destroy_wait(wait_info);
187 }
188
189 static void
190 _tdm_server_cb_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
191                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
192 {
193         _tdm_server_send_done((tdm_server_wait_info*)user_data, error, sequence, tv_sec, tv_usec);
194 }
195
196 static void
197 _tdm_server_cb_output_change(tdm_output *output, tdm_output_change_type type,
198                                                          tdm_value value, void *user_data)
199 {
200         tdm_server_output_info *output_info = user_data;
201         struct wl_client *client;
202         pid_t pid = 0;
203
204         TDM_RETURN_IF_FAIL(output_info != NULL);
205
206         client = wl_resource_get_client(output_info->resource);
207         TDM_RETURN_IF_FAIL(client != NULL);
208
209         wl_client_get_credentials(client, &pid, NULL, NULL);
210
211         if (!output_info->watch_output_changes) {
212                 TDM_DBG("skip sending the output changes: pid(%d)", (unsigned int)pid);
213                 return;
214         }
215
216         TDM_DBG("send the output changes: %d", (unsigned int)pid);
217
218         switch (type) {
219         case TDM_OUTPUT_CHANGE_DPMS:
220                 wl_tdm_output_send_dpms(output_info->resource, value.u32, TDM_ERROR_NONE);
221                 break;
222         case TDM_OUTPUT_CHANGE_CONNECTION:
223                 wl_tdm_output_send_connection(output_info->resource, value.u32, TDM_ERROR_NONE);
224                 break;
225         default:
226                 break;
227         }
228 }
229
230 static void
231 destroy_wait(tdm_server_wait_info *wait_info)
232 {
233         LIST_DEL(&wait_info->link);
234         free(wait_info);
235 }
236
237 static void
238 destroy_vblank_callback(struct wl_resource *resource)
239 {
240         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
241         tdm_server_wait_info *w = NULL, *ww = NULL;
242
243         TDM_RETURN_IF_FAIL(vblank_info != NULL);
244
245         if (vblank_info->vblank)
246                 tdm_vblank_destroy(vblank_info->vblank);
247
248         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &keep_private_server->wait_list, link) {
249                 if (w->vblank_info == vblank_info)
250                         destroy_wait(w);
251         }
252
253         LIST_DEL(&vblank_info->link);
254         free(vblank_info);
255 }
256
257 static void
258 _tdm_server_vblank_cb_destroy(struct wl_client *client, struct wl_resource *resource)
259 {
260         wl_resource_destroy(resource);
261 }
262
263 static void
264 _tdm_server_vblank_cb_set_name(struct wl_client *client, struct wl_resource *resource, const char *name)
265 {
266         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
267
268         tdm_vblank_set_name(vblank_info->vblank, name);
269 }
270
271 static void
272 _tdm_server_vblank_cb_set_fps(struct wl_client *client, struct wl_resource *resource, uint32_t fps)
273 {
274         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
275
276         tdm_vblank_set_fps(vblank_info->vblank, fps);
277 }
278
279 static void
280 _tdm_server_vblank_cb_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t offset)
281 {
282         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
283
284         tdm_vblank_set_offset(vblank_info->vblank, offset);
285 }
286
287 static void
288 _tdm_server_vblank_cb_set_enable_fake(struct wl_client *client, struct wl_resource *resource, uint32_t enable_fake)
289 {
290         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
291
292         tdm_vblank_set_enable_fake(vblank_info->vblank, enable_fake);
293 }
294
295 static void
296 _tdm_server_vblank_cb_wait_vblank(struct wl_client *client, struct wl_resource *resource,
297                                                                   uint32_t interval, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
298 {
299         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
300         tdm_server_output_info *output_info = vblank_info->output_info;
301         tdm_private_server *private_server = output_info->private_server;
302         tdm_server_wait_info *wait_info;
303         unsigned int enable_fake = 0;
304         tdm_error ret;
305
306         TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
307
308         wait_info = calloc(1, sizeof * wait_info);
309         if (!wait_info) {
310                 TDM_ERR("alloc failed");
311                 ret = TDM_ERROR_OUT_OF_MEMORY;
312                 goto wait_failed;
313         }
314
315         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
316         wait_info->vblank_info = vblank_info;
317         wait_info->req_id = req_id;
318
319         if (tdm_debug_module & TDM_DEBUG_VBLANK)
320                 TDM_DBG("req_id(%d) wait", req_id);
321
322         ret = tdm_vblank_wait(vblank_info->vblank, req_sec, req_usec, interval, _tdm_server_cb_vblank, wait_info);
323
324         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
325         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
326                 goto wait_failed;
327
328         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
329
330         return;
331 wait_failed:
332         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
333         if (wait_info)
334                 destroy_wait(wait_info);
335 }
336
337 static void
338 _tdm_server_vblank_cb_wait_vblank_seq(struct wl_client *client, struct wl_resource *resource,
339                                                                           uint32_t sequence, uint32_t req_id, uint32_t req_sec, uint32_t req_usec)
340 {
341         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
342         tdm_server_output_info *output_info = vblank_info->output_info;
343         tdm_private_server *private_server = output_info->private_server;
344         tdm_server_wait_info *wait_info;
345         unsigned int enable_fake = 0;
346         tdm_error ret;
347
348         TDM_TRACE_COUNT(ServerWaitVBlank, req_id);
349
350         wait_info = calloc(1, sizeof * wait_info);
351         if (!wait_info) {
352                 TDM_ERR("alloc failed");
353                 ret = TDM_ERROR_OUT_OF_MEMORY;
354                 goto wait_failed;
355         }
356
357         LIST_ADDTAIL(&wait_info->link, &private_server->wait_list);
358         wait_info->vblank_info = vblank_info;
359         wait_info->req_id = req_id;
360
361         if (tdm_debug_module & TDM_DEBUG_VBLANK)
362                 TDM_DBG("req_id(%d) wait", req_id);
363
364         ret = tdm_vblank_wait_seq(vblank_info->vblank, req_sec, req_usec, sequence, _tdm_server_cb_vblank, wait_info);
365
366         tdm_vblank_get_enable_fake(vblank_info->vblank, &enable_fake);
367         if (!enable_fake && ret == TDM_ERROR_DPMS_OFF)
368                 goto wait_failed;
369
370         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
371
372         return;
373 wait_failed:
374         wl_tdm_vblank_send_done(vblank_info->resource, req_id, 0, 0, 0, ret);
375         if (wait_info)
376                 destroy_wait(wait_info);
377 }
378
379 static const struct wl_tdm_vblank_interface tdm_vblank_implementation = {
380         _tdm_server_vblank_cb_destroy,
381         _tdm_server_vblank_cb_set_name,
382         _tdm_server_vblank_cb_set_fps,
383         _tdm_server_vblank_cb_set_offset,
384         _tdm_server_vblank_cb_set_enable_fake,
385         _tdm_server_vblank_cb_wait_vblank,
386         _tdm_server_vblank_cb_wait_vblank_seq,
387 };
388
389 static void
390 _tdm_server_output_cb_destroy(struct wl_client *client, struct wl_resource *resource)
391 {
392         wl_resource_destroy(resource);
393 }
394
395 static void
396 _tdm_server_output_cb_create_vblank(struct wl_client *client, struct wl_resource *resource, uint32_t id)
397 {
398         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
399         tdm_private_server *private_server = output_info->private_server;
400         tdm_private_loop *private_loop = private_server->private_loop;
401         struct wl_resource *vblank_resource;
402         tdm_vblank *vblank;
403         tdm_server_vblank_info *vblank_info;
404
405         vblank_resource =
406                 wl_resource_create(client, &wl_tdm_vblank_interface,
407                                                    wl_resource_get_version(resource), id);
408         if (!vblank_resource) {
409                 wl_resource_post_no_memory(resource);
410                 TDM_ERR("wl_resource_create failed");
411                 return;
412         }
413
414         vblank = tdm_vblank_create(private_loop->dpy, output_info->output, NULL);
415         if (!vblank) {
416                 wl_resource_post_no_memory(resource);
417                 wl_resource_destroy(vblank_resource);
418                 TDM_ERR("tdm_vblank_create failed");
419                 return;
420         }
421
422         vblank_info = calloc(1, sizeof * vblank_info);
423         if (!vblank_info) {
424                 wl_resource_post_no_memory(resource);
425                 wl_resource_destroy(vblank_resource);
426                 tdm_vblank_destroy(vblank);
427                 TDM_ERR("alloc failed");
428                 return;
429         }
430
431         LIST_ADDTAIL(&vblank_info->link, &output_info->vblank_list);
432         vblank_info->output_info = output_info;
433         vblank_info->resource = vblank_resource;
434         vblank_info->vblank = vblank;
435
436         tdm_vblank_set_resource(vblank, vblank_resource);
437
438         wl_resource_set_implementation(vblank_resource, &tdm_vblank_implementation,
439                                                                    vblank_info, destroy_vblank_callback);
440
441         return;
442 }
443
444 static void
445 _tdm_server_output_cb_watch_output_changes(struct wl_client *client, struct wl_resource *resource, unsigned int enable)
446 {
447         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
448
449         TDM_RETURN_IF_FAIL(output_info != NULL);
450
451         output_info->watch_output_changes = enable;
452 }
453
454 static void
455 _tdm_server_output_cb_get_connection(struct wl_client *client, struct wl_resource *resource)
456 {
457         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
458         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
459         tdm_error ret;
460
461         TDM_RETURN_IF_FAIL(output_info != NULL);
462
463         ret = tdm_output_get_conn_status(output_info->output, &status);
464         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
465
466         wl_tdm_output_send_connection(output_info->resource, status, ret);
467
468         return;
469
470 failed:
471         wl_tdm_output_send_connection(output_info->resource, TDM_OUTPUT_CONN_STATUS_DISCONNECTED, ret);
472 }
473
474 static void
475 _tdm_server_output_cb_get_mode(struct wl_client *client, struct wl_resource *resource)
476 {
477         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
478         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
479         tdm_error ret;
480
481         TDM_RETURN_IF_FAIL(output_info != NULL);
482
483         ret = tdm_output_get_conn_status(output_info->output, &status);
484         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
485
486         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
487                 const tdm_output_mode *mode = NULL;
488                 unsigned int hdisplay, vdisplay, vrefresh;
489
490                 ret = tdm_output_get_mode(output_info->output, &mode);
491                 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
492
493                 hdisplay = (mode) ? mode->hdisplay : 0;
494                 vdisplay = (mode) ? mode->vdisplay : 0;
495                 vrefresh = (mode) ? mode->vrefresh : 0;
496
497                 wl_tdm_output_send_mode(output_info->resource, hdisplay, vdisplay, vrefresh, ret);
498         } else {
499                 wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
500         }
501
502         return;
503 failed:
504         wl_tdm_output_send_mode(output_info->resource, 0, 0, 0, ret);
505 }
506
507 static void
508 _tdm_server_output_cb_get_dpms(struct wl_client *client, struct wl_resource *resource)
509 {
510         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
511         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
512         tdm_error ret;
513
514         TDM_RETURN_IF_FAIL(output_info != NULL);
515
516         ret = tdm_output_get_conn_status(output_info->output, &status);
517         TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
518
519         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
520                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
521
522                 ret = tdm_output_get_dpms(output_info->output, &dpms_value);
523                 TDM_GOTO_IF_FAIL(ret != TDM_ERROR_NONE, failed);
524
525                 wl_tdm_output_send_dpms(output_info->resource, dpms_value, ret);
526         } else {
527                 wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
528         }
529
530         return;
531 failed:
532         wl_tdm_output_send_dpms(output_info->resource, TDM_OUTPUT_DPMS_OFF, ret);
533 }
534
535 static const struct wl_tdm_output_interface tdm_output_implementation = {
536         _tdm_server_output_cb_destroy,
537         _tdm_server_output_cb_create_vblank,
538         _tdm_server_output_cb_watch_output_changes,
539         _tdm_server_output_cb_get_connection,
540         _tdm_server_output_cb_get_mode,
541         _tdm_server_output_cb_get_dpms,
542 };
543
544 static void
545 destroy_output_callback(struct wl_resource *resource)
546 {
547         tdm_server_output_info *output_info = wl_resource_get_user_data(resource);
548         tdm_server_vblank_info *v = NULL, *vv = NULL;
549
550         TDM_RETURN_IF_FAIL(output_info != NULL);
551
552         tdm_output_remove_change_handler(output_info->output,
553                                                                          _tdm_server_cb_output_change, output_info);
554
555         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &output_info->vblank_list, link) {
556                 wl_resource_destroy(v->resource);
557         }
558
559         LIST_DEL(&output_info->link);
560         free(output_info);
561 }
562
563 static void
564 _tdm_server_cb_create_output(struct wl_client *client, struct wl_resource *resource,
565                                                          const char *name, uint32_t id)
566 {
567         tdm_private_server *private_server = wl_resource_get_user_data(resource);
568         tdm_server_output_info *output_info;
569         struct wl_resource *output_resource = NULL;
570         tdm_output *output;
571         tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
572         tdm_error ret;
573
574         output = _tdm_server_find_output(private_server, name);
575         if (!output) {
576                 TDM_ERR("There is no '%s' output", name);
577                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
578                                                            "There is no '%s' output", name);
579                 return;
580         }
581
582         output_resource =
583                 wl_resource_create(client, &wl_tdm_output_interface,
584                                                    wl_resource_get_version(resource), id);
585         if (!output_resource) {
586                 wl_resource_post_no_memory(resource);
587                 TDM_ERR("wl_resource_create failed");
588                 return;
589         }
590
591         output_info = calloc(1, sizeof * output_info);
592         if (!output_info) {
593                 wl_resource_post_no_memory(resource);
594                 wl_resource_destroy(output_resource);
595                 TDM_ERR("alloc failed");
596                 return;
597         }
598
599         LIST_ADDTAIL(&output_info->link, &private_server->output_list);
600         output_info->private_server = private_server;
601         output_info->resource = output_resource;
602         output_info->output = output;
603         LIST_INITHEAD(&output_info->vblank_list);
604
605         tdm_output_add_change_handler(output, _tdm_server_cb_output_change, output_info);
606
607         wl_resource_set_implementation(output_resource, &tdm_output_implementation,
608                                                                    output_info, destroy_output_callback);
609
610         ret = tdm_output_get_conn_status(output, &status);
611         wl_tdm_output_send_connection(output_resource, status, ret);
612
613         if (status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
614                 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_OFF;
615                 const tdm_output_mode *mode = NULL;
616                 unsigned int hdisplay, vdisplay, vrefresh;
617
618                 ret = tdm_output_get_mode(output, &mode);
619                 hdisplay = (mode) ? mode->hdisplay : 0;
620                 vdisplay = (mode) ? mode->vdisplay : 0;
621                 vrefresh = (mode) ? mode->vrefresh : 0;
622                 wl_tdm_output_send_mode(output_resource, hdisplay, vdisplay, vrefresh, ret);
623
624                 ret = tdm_output_get_dpms(output, &dpms_value);
625                 wl_tdm_output_send_dpms(output_resource, dpms_value, ret);
626         } else {
627                 wl_tdm_output_send_mode(output_resource, 0, 0, 0, TDM_ERROR_OUTPUT_DISCONNECTED);
628                 wl_tdm_output_send_dpms(output_resource, TDM_OUTPUT_DPMS_OFF, TDM_ERROR_OUTPUT_DISCONNECTED);
629         }
630 }
631
632 static void
633 _tdm_server_cb_debug(struct wl_client *client, struct wl_resource *resource, const char *options)
634 {
635         tdm_private_server *private_server = wl_resource_get_user_data(resource);
636         tdm_private_loop *private_loop = private_server->private_loop;
637         char message[TDM_SERVER_REPLY_MSG_LEN];
638         char *m;
639         int len = sizeof(message), size;
640         uid_t uid;
641
642         wl_client_get_credentials(client, NULL, &uid, NULL);
643
644         if (uid != 0) {
645                 snprintf(message, len, "tdm-monitor: SHOULD be a superuser.\n");
646                 TDM_ERR("%s", message);
647         } else {
648                 tdm_monitor_server_command(private_loop->dpy, options, message, &len);
649         }
650
651         size = sizeof(message) - len;
652         m = message;
653
654         wl_client_flush(client);
655
656         while (size > 0) {
657                 char buffer[TDM_DEBUG_REPLY_MSG_LEN];
658                 int copylen = TDM_MIN(size, sizeof(buffer) - 1);
659
660                 strncpy(buffer, m, copylen);
661                 m += copylen;
662                 size -= copylen;
663
664                 buffer[copylen] = '\0';
665
666                 wl_tdm_send_debug_message(resource, buffer);
667         }
668
669         wl_tdm_send_debug_done(resource);
670 }
671
672 static void
673 _tdm_server_cb_set_client_vblank_fps(struct wl_client *client, struct wl_resource *resource,
674                                                                          unsigned int pid, const char *name, unsigned int fps)
675 {
676         tdm_error ret = tdm_vblank_set_client_vblank_fps(pid, name, fps);
677         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
678
679         TDM_INFO("'%s' vblank fps(PID: '%u'): %u", name, pid, fps);
680 }
681
682 static const struct wl_tdm_interface tdm_implementation = {
683         _tdm_server_cb_debug,
684         _tdm_server_cb_create_output,
685         _tdm_server_cb_set_client_vblank_fps,
686 };
687
688 static void
689 destroy_client(struct wl_resource *resource)
690 {
691         tdm_server_client_info *c = NULL, *cc = NULL;
692         struct wl_client *client;
693         pid_t pid = -1;
694
695         client = wl_resource_get_client(resource);
696         TDM_RETURN_IF_FAIL(client != NULL);
697
698         wl_client_get_credentials(client, &pid, NULL, NULL);
699
700         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
701                 if (c->pid == pid) {
702                         LIST_DEL(&c->link);
703                         free(c);
704                         return;
705                 }
706         }
707 }
708
709 static void
710 _tdm_server_bind(struct wl_client *client, void *data,
711                                  uint32_t version, uint32_t id)
712 {
713         struct wl_resource *resource;
714         tdm_server_client_info *cinfo;
715
716         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
717         if (!resource) {
718                 wl_client_post_no_memory(client);
719                 return;
720         }
721
722         cinfo = calloc(1, sizeof(tdm_server_client_info));
723         if (!cinfo) {
724                 wl_client_post_no_memory(client);
725                 wl_resource_destroy(resource);
726                 return;
727         }
728
729         LIST_ADDTAIL(&cinfo->link, &client_list);
730         wl_client_get_credentials(client, &cinfo->pid, NULL, NULL);
731         _tdm_server_get_process_name(cinfo->pid, cinfo->name, TDM_NAME_LEN);
732
733         wl_resource_set_implementation(resource, &tdm_implementation, data, destroy_client);
734 }
735
736 static int
737 _tdm_getgrnam_r(const char *name)
738 {
739         struct group *grp = NULL;
740         struct group *grp_res = NULL;
741         char* buf = NULL;
742         size_t buf_len;
743         int ret;
744         int id;
745
746         buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
747         if (buf_len == -1)
748                 buf_len = 2048;
749
750         buf = calloc(1, buf_len * sizeof(char));
751         if (!buf) {
752                 TDM_ERR("creating buffer failed");
753                 goto failed;
754         }
755
756         grp = calloc(1, sizeof(struct group));
757         if (!grp) {
758                 TDM_ERR("creating group failed");
759                 goto failed;
760         }
761
762         ret = getgrnam_r(name, grp, buf, buf_len, &grp_res);
763         if (ret < 0) {
764                 TDM_ERR("getgrnam_r failed errno:%d(%m)", ret);
765                 goto failed;
766         }
767
768         if (grp_res == NULL) {
769                 TDM_ERR("finding name:%s group failed", name);
770                 goto failed;
771         }
772
773         id = grp->gr_gid;
774         free(buf);
775         free(grp);
776
777         return id;
778
779 failed:
780         if (buf)
781                 free(buf);
782         if (grp)
783                 free(grp);
784
785         return -1;
786 }
787
788 static void
789 _tdm_socket_init(tdm_private_loop *private_loop)
790 {
791         const char *dir = NULL;
792         char socket_path[128];
793         int size;
794         int ret = -1;
795         uid_t uid;
796         gid_t gid;
797
798         dir = getenv("XDG_RUNTIME_DIR");
799         if (!dir) {
800                 TDM_WRN("getting XDG_RUNTIME_DIR failed");
801                 return;
802         }
803
804         size = snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, "tdm-socket");
805         if (size >= (int)sizeof(socket_path)) {
806                 TDM_WRN("too long: %s/tdm-socket", dir);
807                 return;
808         };
809
810         ret = chmod(socket_path, 509);
811         if (ret < 0) {
812                 TDM_WRN("changing modes of socket file failed:%s (%m)", socket_path);
813                 return;
814         }
815
816         ret = _tdm_getgrnam_r("root");
817         if (ret < 0) {
818                 TDM_WRN("getting uid failed");
819                 return;
820         }
821         uid = ret;
822
823         ret = _tdm_getgrnam_r("display");
824         if (ret < 0) {
825                 TDM_WRN("getting gid failed");
826                 return;
827         }
828         gid = ret;
829
830         ret = chown(socket_path, uid, gid);
831         if (ret < 0) {
832                 TDM_WRN("changing owner of socket file failed:%s (%m)", socket_path);
833                 return;
834         }
835 }
836
837 INTERN tdm_error
838 tdm_server_init(tdm_private_loop *private_loop)
839 {
840         tdm_private_server *private_server;
841
842         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
843         TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
844
845         if (private_loop->private_server)
846                 return TDM_ERROR_NONE;
847
848         if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
849                 TDM_ERR("createing a tdm-socket failed");
850                 return TDM_ERROR_OPERATION_FAILED;
851         }
852
853         _tdm_socket_init(private_loop);
854
855         private_server = calloc(1, sizeof * private_server);
856         if (!private_server) {
857                 TDM_ERR("alloc failed");
858                 return TDM_ERROR_OUT_OF_MEMORY;
859         }
860
861         LIST_INITHEAD(&private_server->output_list);
862         LIST_INITHEAD(&private_server->wait_list);
863
864         if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
865                                                   private_server, _tdm_server_bind)) {
866                 TDM_ERR("creating a global resource failed");
867                 free(private_server);
868                 return TDM_ERROR_OUT_OF_MEMORY;
869         }
870
871         private_server->private_loop = private_loop;
872         private_loop->private_server = private_server;
873         keep_private_server = private_server;
874
875         LIST_INITHEAD(&client_list);
876
877         return TDM_ERROR_NONE;
878 }
879
880 INTERN void
881 tdm_server_deinit(tdm_private_loop *private_loop)
882 {
883         tdm_server_output_info *o = NULL, *oo = NULL;
884         tdm_server_wait_info *w = NULL, *ww = NULL;
885         tdm_server_client_info *c = NULL, *cc = NULL;
886         tdm_private_server *private_server;
887
888         if (!private_loop->private_server)
889                 return;
890
891         private_server = private_loop->private_server;
892
893         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_server->wait_list, link) {
894                 destroy_wait(w);
895         }
896
897         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_server->output_list, link) {
898                 wl_resource_destroy(o->resource);
899         }
900
901         free(private_server);
902         private_loop->private_server = NULL;
903         keep_private_server = NULL;
904
905         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &client_list, link) {
906                 LIST_DEL(&c->link);
907                 free(c);
908         }
909 }
910
911 INTERN const char*
912 tdm_server_get_client_name(pid_t pid)
913 {
914         tdm_server_client_info *c = NULL;
915
916         LIST_FOR_EACH_ENTRY(c, &client_list, link) {
917                 if (c->pid == pid)
918                         return (const char*)c->name;
919         }
920
921         return NULL;
922 }