Fix invalid type specifier
[platform/core/api/system-info.git] / src / update_db / system_info_db_update.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <getopt.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <errno.h>
30
31 #define KEY_MAX 256
32
33 static const char *db_update_arg[] = {
34         "/usr/bin/system_info_init_db",
35         "-k", NULL,
36         "-t", NULL,
37         "-g", NULL,
38         "-v", NULL,
39         NULL,
40 };
41
42 static void show_help(void)
43 {
44         printf("system_info_update_db [OPTIONS]\n");
45         printf("  -h --help         Show this help\n");
46         printf("  -r --root=ROOT    Root path for chroot operation\n");
47         printf("  -k --key=KEY      System info key to update\n");
48         printf("  -t --type=TYPE    System info type to update (int/bool/double/string)\n");
49         printf("  -g --tag=TAGE     System info tag to update (platform/custom)\n");
50         printf("  -v --value=VALUE  System info value to update\n");
51 }
52
53 int main(int argc, char *argv[])
54 {
55         pid_t pid;
56         int status, ret;
57         int opt;
58         bool failed;
59         char root[KEY_MAX] = { 0, };
60         char key[KEY_MAX] = { 0, };
61         char type[KEY_MAX] = { 0, };
62         char tag[KEY_MAX] = { 0, };
63         char value[KEY_MAX] = { 0, };
64         struct option long_options[] = {
65                 { "root",  required_argument, 0, 0 },
66                 { "key",   required_argument, 0, 0 },
67                 { "type",  required_argument, 0, 0 },
68                 { "tag",   required_argument, 0, 0 },
69                 { "value", required_argument, 0, 0 },
70                 { "help",  no_argument,       0, 0 },
71                 { 0,       0,                 0, 0 },
72         };
73
74         while (1) {
75                 opt = getopt_long(argc, argv, "r:k:t:g:v:h",
76                                 long_options, NULL);
77                 if (opt < 0)
78                         break;
79                 switch (opt) {
80                 case 'r':
81                         snprintf(root, sizeof(root), "%s", optarg);
82                         break;
83                 case 'k':
84                         snprintf(key, sizeof(key), "%s", optarg);
85                         break;
86                 case 't':
87                         snprintf(type, sizeof(type), "%s", optarg);
88                         break;
89                 case 'g':
90                         snprintf(tag, sizeof(tag), "%s", optarg);
91                         break;
92                 case 'v':
93                         snprintf(value, sizeof(value), "%s", optarg);
94                         break;
95                 case 'h':
96                 default:
97                         show_help();
98                         return 0;
99                 }
100         }
101
102         failed = false;
103         if (root[0] == '\0') {
104                 printf("Invalid Parameter: no root path\n");
105                 failed = true;
106         }
107         if (key[0] == '\0') {
108                 printf("Invalid Parameter: no key\n");
109                 failed = true;
110         }
111         if (type[0] == '\0') {
112                 printf("Invalid Parameter: no type\n");
113                 failed = true;
114         }
115         if (tag[0] == '\0') {
116                 printf("Invalid Parameter: no tag\n");
117                 failed = true;
118         }
119         if (value[0] == '\0') {
120                 printf("Invalid Parameter: no value\n");
121                 failed = true;
122         }
123
124         if (failed)
125                 return -EINVAL;
126
127         printf("Root Path(%s)\n", root);
128         printf("Request to update: key(%s), type(%s), tag(%s), value(%s)\n",
129                         key, type, tag, value);
130
131         db_update_arg[2] = key;
132         db_update_arg[4] = type;
133         db_update_arg[6] = tag;
134         db_update_arg[8] = value;
135
136         ret = chroot(root);
137         if (ret < 0) {
138                 printf("Failed to change root dir (%s, errno:%d)\n", root, errno);
139                 return -errno;
140         }
141         ret = chdir("/");
142         if (ret < 0) {
143                 printf("Failed to go to root directory : %d", errno);
144                 return -errno;
145         }
146
147         pid = fork();
148         if (pid < 0) {
149                 printf("Failed to update db (%d)\n", errno);
150                 return -errno;
151         } else if (pid == 0) {
152                 ret = execv(db_update_arg[0], (char **)db_update_arg);
153                 if (ret < 0)
154                         exit(EXIT_FAILURE);
155                 return ret;
156         } else {
157                 ret = waitpid(pid, &status, 0);
158                 if (ret == -1) {
159                         printf("%d waitpid() failed : %d\n", pid, errno);
160                         return -EAGAIN;
161                 }
162
163                 if (WIFEXITED(status)) {
164                         printf("%d terminated by exit(%d)\n", pid, WEXITSTATUS(status));
165                         return WEXITSTATUS(status);
166                 }
167
168                 if (WIFSIGNALED(status))
169                         printf("%d terminated by signal %d\n", pid, WTERMSIG(status));
170                 else if (WIFSTOPPED(status))
171                         printf("%d stopped by signal %d\n", pid, WSTOPSIG(status));
172                 return -EAGAIN;
173         }
174 }