Add default Smack manifest for devman.spec
[platform/core/system/devman.git] / devman_internal.c
1 /*
2  *  devman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  * 
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20 */ 
21
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <dirent.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 #include <poll.h>
37
38 #include "devlog.h"
39 #include "devman_internal.h"
40
41 #define WD_RESPONSE_TIMEOUT     100 /* 0.1 seconds */
42 #define DISPLAY_WD_PATH                 "/usr/bin/display_wd"
43
44 static int fifo_fd;
45
46 API int display_register_postjob(void)
47 {
48         int ret, i;
49         long open_max;
50         pid_t pid;
51         char buf[PATH_MAX];
52         char fifo_path[NAME_MAX];
53         struct pollfd fifo_pollfd;
54
55         snprintf(fifo_path, NAME_MAX, "%s.%d", DISPLAY_WD_FIFO, getpid());
56         if (access(fifo_path, F_OK) == 0) {
57                 ERR("Already registered!");
58                 return -1;
59         }
60         mkfifo(fifo_path, 0700);
61
62         pid = fork();
63         if(pid < 0) {
64                 ERR("Failed to fork child process for LCD On/Off");
65                 unlink(fifo_path);
66                 return -1;
67         }
68         if (pid == 0) {
69                 open_max = sysconf(_SC_OPEN_MAX);
70                 for (i = 0; i < open_max; i++) {
71                         close(i);
72                 }
73
74                 execl(DISPLAY_WD_PATH, DISPLAY_WD_PATH, NULL);
75         }
76
77         fifo_pollfd.fd = open(fifo_path, O_RDWR | O_NONBLOCK);
78         if (fifo_pollfd.fd < 0) {
79                 ERR("Cannot open fifo file");
80                 unlink(fifo_path);
81                 return -1;
82         }
83
84         /* get the watch dog ready message. */
85         fifo_pollfd.events = POLLIN;
86         if (poll(&fifo_pollfd, 1, WD_RESPONSE_TIMEOUT) < 0) {
87                 ERR("Cannot poll the fifo file");
88                 DBG("fifo file path is %s", fifo_path);
89                 close(fifo_pollfd.fd);
90                 unlink(fifo_path);
91                 return -1;
92         }
93         read(fifo_pollfd.fd, buf, sizeof(buf));
94
95         fifo_fd = fifo_pollfd.fd;
96
97         return 0;
98 }
99
100 API int display_cancel_postjob(void)
101 {
102         char buf[PATH_MAX];
103         int ret;
104
105         snprintf(buf, PATH_MAX, "%s.%d", DISPLAY_WD_FIFO, getpid());
106         if (access(buf, F_OK) != 0) {
107                 ERR("No registered the post job!");
108                 return -1;
109         }
110
111         if (fifo_fd < 0)
112                 fifo_fd = open(buf, O_WRONLY);
113         if (fifo_fd < 0) {
114                 ERR("Cannot open the fifo file");
115                 DBG("fifo file path is %s", buf);
116                 return -1;
117         }
118         ret = DISPLAY_WD_CANCEL;       
119         write(fifo_fd, &ret, sizeof(int));
120         close(fifo_fd);
121         unlink(buf);
122         fifo_fd = -1;
123
124         return 0;
125 }
126