[FIX] empty string in sprintf
[platform/core/system/swap-manager.git] / daemon / buffer.c
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
9  * Nikita Kalyazin    <n.kalyazin@samsung.com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Contributors:
24  * - Samsung RnD Institute Russia
25  *
26  */
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "daemon.h"
36 #include "buffer.h"
37 #include "ioctl_commands.h"
38 #include "debug.h"
39
40 #define SUBBUF_SIZE 64 * 1024
41 #define SUBBUF_NUM 32 * 16
42
43 static int open_buf_ctl(void)
44 {
45         manager.buf_fd = open(BUF_FILENAME, O_RDONLY);
46         if (manager.buf_fd == -1) {
47                 LOGE("Cannot open buffer: %s\n", strerror(errno));
48                 return 1;
49         }
50         LOGI("buffer opened: %s, %d\n", BUF_FILENAME, manager.buf_fd);
51
52         manager.user_ev_fd = open(USER_EVENT_FILENAME, O_WRONLY);
53         if (manager.user_ev_fd == -1) {
54                 LOGE("Cannot open user event sysfs file: %s\b", strerror(errno));
55                 return 1;
56         }
57         LOGI("user event sysfs file opened: %s, %d\n", USER_EVENT_FILENAME,
58              manager.user_ev_fd);
59
60         return 0;
61 }
62
63 static void close_buf_ctl(void)
64 {
65         LOGI("close buffer (%d)\n", manager.buf_fd);
66         close(manager.buf_fd);
67
68         LOGI("close user event sysfs file (%d)\n", manager.user_ev_fd);
69         close(manager.user_ev_fd);
70 }
71
72 static int insert_buf_modules(void)
73 {
74         if (system("cd /opt/swap/sdk && ./start.sh")) {
75                 LOGE("Cannot insert swap modules\n");
76                 return -1;
77         }
78
79         return 0;
80 }
81
82 int init_buf(void)
83 {
84         struct buffer_initialize init = {
85                 .size = SUBBUF_SIZE,
86                 .count = SUBBUF_NUM,
87         };
88
89         if (insert_buf_modules() != 0) {
90                 LOGE("Cannot insert buffer modules\n");
91                 return 1;
92         }
93
94         if (open_buf_ctl() != 0) {
95                 LOGE("Cannot open buffer\n");
96                 return 1;
97         }
98
99         if (ioctl(manager.buf_fd, SWAP_DRIVER_BUFFER_INITIALIZE, &init) == -1) {
100                 LOGE("Cannot init buffer: %s\n", strerror(errno));
101                 return 1;
102         }
103
104         return 0;
105 }
106
107 void exit_buf(void)
108 {
109         LOGI("Uninit driver (%d)\n", manager.buf_fd);
110         if (ioctl(manager.buf_fd, SWAP_DRIVER_BUFFER_UNINITIALIZE) == -1)
111                 LOGW("Cannot uninit driver: %s\n", strerror(errno));
112
113         close_buf_ctl();
114 }
115
116 void flush_buf(void)
117 {
118         if (ioctl(manager.buf_fd, SWAP_DRIVER_FLUSH_BUFFER) == -1)
119                 LOGW("Cannot send flush to driver: %s\n", strerror(errno));
120 }
121
122 void wake_up_buf(void)
123 {
124         if (ioctl(manager.buf_fd, SWAP_DRIVER_WAKE_UP) == -1)
125                 LOGW("Cannot send wake up to driver: %s\n", strerror(errno));
126 }
127
128 int write_to_buf(struct msg_data_t *msg)
129 {
130         if (write(manager.user_ev_fd, msg, MSG_DATA_HDR_LEN + msg->len) == -1) {
131                 LOGE("write to buf: %s\n", strerror(errno));
132                 return 1;
133         }
134         return 0;
135 }