udev: Increase udev monitor buffer size
[platform/adaptation/RPI3/device-manager-plugin-RPI3.git] / hw / shared.c
1 /*
2  * device-node
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <errno.h>
28
29 #define BUF_MAX         255
30
31 static int sys_read_buf(char *file, char *buf, int len)
32 {
33         int fd, r;
34
35         if (!file || !buf || len < 0)
36                 return -EINVAL;
37
38         fd = open(file, O_RDONLY);
39         if (fd == -1)
40                 return -ENOENT;
41
42         r = read(fd, buf, len);
43         close(fd);
44         if ((r >= 0) && (r < len))
45                 buf[r] = '\0';
46         else
47                 return -EIO;
48
49         return 0;
50 }
51
52 static int sys_write_buf(char *file, char *buf)
53 {
54         int fd, r;
55
56         if (!file || !buf)
57                 return -EINVAL;
58
59         fd = open(file, O_WRONLY);
60         if (fd == -1)
61                 return -EPERM;
62
63         r = write(fd, buf, strlen(buf));
64         close(fd);
65         if (r < 0)
66                 return -EIO;
67
68         return 0;
69 }
70
71 int sys_get_int(char *fname, int *val)
72 {
73         char buf[BUF_MAX];
74         int r;
75
76         if (!fname || !val)
77                 return -EINVAL;
78
79         r = sys_read_buf(fname, buf, sizeof(buf));
80         if (r < 0)
81                 return r;
82
83         *val = atoi(buf);
84         return 0;
85 }
86
87 int sys_get_str(char *fname, char *str, int len)
88 {
89         int r;
90
91         if (!fname || !str || len < 0)
92                 return -EINVAL;
93
94         r = sys_read_buf(fname, str, len);
95         if (r < 0)
96                 return r;
97
98         return 0;
99 }
100
101 int sys_set_int(char *fname, int val)
102 {
103         char buf[BUF_MAX];
104         int r;
105
106         if (!fname)
107                 return -EINVAL;
108
109         snprintf(buf, sizeof(buf), "%d", val);
110         r = sys_write_buf(fname, buf);
111         if (r < 0)
112                 return r;
113
114         return 0;
115 }
116
117 int sys_set_str(char *fname, char *val)
118 {
119         int r;
120
121         if (!fname || !val)
122                 return -EINVAL;
123
124         r = sys_write_buf(fname, val);
125         if (r < 0)
126                 return r;
127
128         return 0;
129 }