4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hakjoo Ko <hakjoo.ko@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <glib-object.h>
28 #include "vconf-log.h"
32 #include <sys/types.h>
39 VCONFTOOL_TYPE_NO = 0x00,
40 VCONFTOOL_TYPE_STRING,
42 VCONFTOOL_TYPE_DOUBLE,
48 const char *BACKEND_DB_PREFIX = "db/";
49 const char *BACKEND_FILE_PREFIX = "file/";
50 const char *BACKEND_MEMORY_PREFIX = "memory/";
52 const char *DB_PREFIX = "/opt/var/kdb";
53 const char *FILE_PREFIX = "/opt/var/kdb";
54 const char *MEMORY_PREFIX = "/var/run";
55 const char *MEMORY_INIT = "/opt/var/kdb/memory_init";
57 static char *guid = NULL;
58 static char *uid = NULL;
59 static char *vconf_type = NULL;
60 static int is_recursive = FALSE;
61 static int is_initialization = FALSE;
62 static int is_forced = FALSE;
63 static int get_num = 0;
65 static GOptionEntry entries[] = {
66 {"type", 't', 0, G_OPTION_ARG_STRING, &vconf_type, "type of value",
67 "int|bool|double|string"},
68 {"recursive", 'r', 0, G_OPTION_ARG_NONE, &is_recursive,
69 "retrieve keys recursively", NULL},
70 {"guid", 'g', 0, G_OPTION_ARG_STRING, &guid, "group permission", NULL},
71 {"uid", 'u', 0, G_OPTION_ARG_STRING, &uid, "user permission", NULL},
72 {"initialization", 'i', 0, G_OPTION_ARG_NONE, &is_initialization,
73 "memory backend initialization", NULL},
74 {"force", 'f', 0, G_OPTION_ARG_NONE, &is_forced,
75 "overwrite vconf values by force", NULL},
79 static void get_operation(char *input);
80 static void recursive_get(char *subDIR, int level);
81 static void print_keylist(keylist_t *keylist, keynode_t *temp_node, int level);
83 static void print_help(const char *cmd)
85 fprintf(stderr, "Usage:\n");
86 fprintf(stderr, "\n");
87 fprintf(stderr, "[Set vconf value]\n");
89 " %s set -t <TYPE> <KEY NAME> <VALUE> <OPTIONS>\n", cmd);
90 fprintf(stderr, " <TYPE>=int|bool|double|string\n");
91 fprintf(stderr, "\n");
93 " Ex) %s set -t string db/testapp/key1 \"This is test\" \n",
95 fprintf(stderr, "\n");
96 fprintf(stderr, " <OPTIONS>\n");
98 " -g <GUID> : Set Effective group id. The key's permission will be set to 0664.\n");
100 " Ex) %s set -t string db/testapp/key1 \"This is test\" -g 425\n",
103 " NOTE: -g and -u cannot be userd together. -u ignores -g option.\n");
104 fprintf(stderr, "\n");
106 " -u <UID> : Set Effective user id. The key's permission will be set to 0644.\n");
108 " Ex) %s set -t string db/testapp/key1 \"This is test\" -u 5000\n",
111 " NOTE: -g and -u cannot be userd together. -u ignores -g option.\n");
112 fprintf(stderr, "\n");
114 " -i : Install memory backend key into flash space for backup.\n");
116 " Ex) %s set -t string db/testapp/key1 \"This is test\" -i\n",
118 fprintf(stderr, "\n");
120 " -f : Overwrite values by force, even when vconf values are already exist.\n");
121 fprintf(stderr, "\n");
122 fprintf(stderr, "[Get vconf value]\n");
123 fprintf(stderr, " %s get <OPTIONS> <KEY NAME>\n", cmd);
124 fprintf(stderr, "\n");
125 fprintf(stderr, " <OPTIONS>\n");
127 " -r : retrieve all keys included in sub-directorys \n");
128 fprintf(stderr, " Ex) %s get db/testapp/key1\n", cmd);
129 fprintf(stderr, " %s get db/testapp/\n", cmd);
130 fprintf(stderr, "\n");
131 fprintf(stderr, "[Unset vconf value]\n");
132 fprintf(stderr, " %s unset <KEY NAME>\n", cmd);
133 fprintf(stderr, "\n");
134 fprintf(stderr, " Ex) %s unset db/testapp/key1\n", cmd);
135 fprintf(stderr, "\n");
138 static int check_type(void)
141 if (!strncasecmp(vconf_type, "int", 3))
142 return VCONFTOOL_TYPE_INT;
143 else if (!strncasecmp(vconf_type, "string", 6))
144 return VCONFTOOL_TYPE_STRING;
145 else if (!strncasecmp(vconf_type, "double", 6))
146 return VCONFTOOL_TYPE_DOUBLE;
147 else if (!strncasecmp(vconf_type, "bool", 4))
148 return VCONFTOOL_TYPE_BOOL;
150 return VCONFTOOL_TYPE_NO;
153 static int __system(char * cmd)
158 if((cpid = fork()) < 0) {
171 execv(w[0], (char *const *)w);
178 if (waitpid(cpid, &status, 0) == -1) {
179 perror("waitpid failed");
182 if (WIFSIGNALED(status)) {
183 printf("signal(%d)\n", WTERMSIG(status));
184 perror("exit by signal");
187 if (!WIFEXITED(status)) {
188 perror("exit abnormally");
191 if (WIFEXITED(status) && WEXITSTATUS(status)) {
192 perror("child return error");
200 static void disable_invalid_char(char* src)
204 for(tmp = src; *tmp; ++tmp)
206 if( (*tmp == ';') || (*tmp == '|') )
208 fprintf(stderr,"invalid char is found\n");
214 static int check_file_path_mode(char* file_path)
216 char szCmd[BUFSIZE] = {0,};
224 "Error!\t Only root user can use '-g or -u' option\n");
231 /* Check file path */
232 if (access(file_path, F_OK) != 0) {
233 /* fprintf(stderr,"key does not exist\n"); */
235 char szPath[BUFSIZE] = { 0, };
236 char *pCh = strrchr(file_path, '/');
237 strncat(szPath, file_path, pCh - file_path);
238 /* fprintf(stderr, "szPath : %s\n", szPath); */
240 /* Check directory & create it */
241 if (access(szPath, F_OK) != 0) {
242 /* fprintf(stderr,"parent dir does not exist\n"); */
244 snprintf(szCmd, BUFSIZE, "/bin/mkdir %s -p --mode=755", szPath);
245 disable_invalid_char(szCmd);
246 if (__system(szCmd)) {
247 fprintf(stderr,"Fail mkdir() szCmd=%s\n", __FILE__, __LINE__, szCmd);
254 } else if (!is_forced) {
255 fprintf(stderr, "Key already exist. Use -f option to force update\n");
263 fd = open(file_path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
266 fprintf(stderr, "open(rdwr,create) error\n");
273 if((fd = open(file_path, O_RDONLY)) == -1) {
274 fprintf(stderr, "open(rdonly) error\n");
279 if (fchown(fd, atoi(uid), atoi(uid)) == -1) {
280 fprintf(stderr, "Error!\t Fail to fchown(%d)\n", errno);
285 if (-1 == fchown(fd, 0, atoi(guid))) {
286 fprintf(stderr, "Error!\t Fail to fchown()\n");
298 static int copy_memory_key(char *pszKey, char *pszOrigin)
300 char szCmd[BUFSIZE] = { 0, };
301 char szPath[BUFSIZE] = { 0, };
302 char szFileName[BUFSIZE] = { 0, };
303 char *pCh = strrchr(pszKey, '/');
304 int nLen = strlen(pszKey);
306 /* Get directory path and file name */
307 snprintf(szPath, BUFSIZE, "%s/", MEMORY_INIT);
308 strncat(szPath, pszKey, pCh - pszKey);
309 strncpy(szFileName, pszKey + (pCh - pszKey) + 1, nLen - (pCh - pszKey));
311 /* Check directory & create it */
312 if (0 != access(szPath, F_OK)) {
313 snprintf(szCmd, BUFSIZE, "mkdir %s -p --mode=755", szPath);
315 printf("[%s:%d]Fail mkdir() szCmd=%s\n", __FILE__,
321 strncat(szPath, "/", 1);
322 strncat(szPath, szFileName, strlen(szFileName));
323 memset(szCmd, 0x00, BUFSIZE);
324 snprintf(szCmd, BUFSIZE, "cp %s %s -r -p", pszOrigin, szPath);
326 printf("[%s:%d]Fail copy\n", __FILE__, __LINE__);
332 static int make_file_path(char *pszKey, char *pszBuf)
335 strncmp(pszKey, BACKEND_DB_PREFIX, sizeof(BACKEND_DB_PREFIX) - 1)) {
336 snprintf(pszBuf, BUFSIZE, "%s/%s", DB_PREFIX, pszKey);
339 strncmp(pszKey, BACKEND_FILE_PREFIX,
340 sizeof(BACKEND_FILE_PREFIX) - 1)) {
341 snprintf(pszBuf, BUFSIZE, "%s/%s", FILE_PREFIX, pszKey);
344 strncmp(pszKey, BACKEND_MEMORY_PREFIX,
345 sizeof(BACKEND_MEMORY_PREFIX) - 1)) {
346 snprintf(pszBuf, BUFSIZE, "%s/%s", MEMORY_PREFIX, pszKey);
352 int main(int argc, char **argv)
355 char szFilePath[BUFSIZE] = { 0, };
360 GError *error = NULL;
361 GOptionContext *context;
364 context = g_option_context_new("- vconf library tool");
365 g_option_context_add_main_entries(context, entries, NULL);
366 g_option_context_set_help_enabled(context, FALSE);
367 g_option_context_set_ignore_unknown_options(context, TRUE);
369 if (!g_option_context_parse(context, &argc, &argv, &error)) {
370 g_print("option parsing failed: %s\n", error->message);
379 if (!strncmp(argv[1], "set", 3)) {
380 set_type = check_type();
381 if (argc < 4 || !set_type) {
386 if (make_file_path(argv[2], szFilePath)) {
387 fprintf(stderr, "Error!\t Bad prefix\n");
391 if (check_file_path_mode(szFilePath)) {
392 fprintf(stderr, "Error!\t create key %s\n", argv[2]);
397 case VCONFTOOL_TYPE_STRING:
398 vconf_set_str(argv[2], argv[3]);
400 case VCONFTOOL_TYPE_INT:
401 vconf_set_int(argv[2], atoi(argv[3]));
403 case VCONFTOOL_TYPE_DOUBLE:
404 vconf_set_dbl(argv[2], atof(argv[3]));
406 case VCONFTOOL_TYPE_BOOL:
407 vconf_set_bool(argv[2], !!atoi(argv[3]));
410 fprintf(stderr, "never reach");
414 /* Install memory backend key into flash space *******/
415 if (is_initialization) {
416 copy_memory_key(argv[2], szFilePath);
418 /* End memory backend key into flash space ***********/
420 } else if (!strncmp(argv[1], "get", 3)) {
422 get_operation(argv[2]);
425 } else if (!strncmp(argv[1], "unset", 5)) {
427 vconf_unset(argv[2]);
431 fprintf(stderr, "%s is a invalid command\n", argv[1]);
435 static void get_operation(char *input)
437 keylist_t *get_keylist;
438 keynode_t *temp_node;
441 get_keylist = vconf_keylist_new();
442 /* ParentDIR parameter of gconf_client_all_entries
443 can not include the last slash. */
444 if ('/' == input[strlen(input) - 1] && strlen(input) > 8)
445 input[strlen(input) - 1] = '\0';
447 vconf_get(get_keylist, input, VCONF_GET_ALL);
448 if (!(temp_node = vconf_keylist_nextnode(get_keylist))) {
449 test = strrchr(input, '/');
451 vconf_keylist_add_null(get_keylist, input);
452 if (test - input < 7)
456 vconf_get(get_keylist, input, VCONF_GET_KEY);
457 temp_node = vconf_keylist_nextnode(get_keylist);
459 fprintf(stderr, "Include at least one slash\"/\"\n");
460 vconf_keylist_free(get_keylist);
465 print_keylist(get_keylist, temp_node, 0);
469 vconf_keylist_free(get_keylist);
472 static void recursive_get(char *subDIR, int level)
474 printf("%s", subDIR);
476 keylist_t *get_keylist;
477 keynode_t *first_node;
479 get_keylist = vconf_keylist_new();
480 vconf_get(get_keylist, subDIR, VCONF_GET_ALL);
482 if ((first_node = vconf_keylist_nextnode(get_keylist))) {
483 print_keylist(get_keylist, first_node, level);
485 vconf_keylist_free(get_keylist);
488 static void print_keylist(keylist_t *keylist, keynode_t *temp_node, int level)
491 switch (vconf_keynode_get_type(temp_node))
494 printf("%s, value = %d (int)\n",
495 vconf_keynode_get_name(temp_node),
496 vconf_keynode_get_int(temp_node));
499 case VCONF_TYPE_BOOL:
500 printf("%s, value = %d (bool)\n",
501 vconf_keynode_get_name(temp_node),
502 vconf_keynode_get_bool(temp_node));
505 case VCONF_TYPE_DOUBLE:
506 printf("%s, value = %f (double)\n",
507 vconf_keynode_get_name(temp_node),
508 vconf_keynode_get_dbl(temp_node));
511 case VCONF_TYPE_STRING:
512 printf("%s, value = %s (string)\n",
513 vconf_keynode_get_name(temp_node),
514 vconf_keynode_get_str(temp_node));
518 printf("%s(Directory)\n",
519 vconf_keynode_get_name(temp_node));
521 recursive_get(vconf_keynode_get_name(temp_node),
525 /* fprintf(stderr, "Unknown Type(%d)\n", vconf_keynode_get_type(temp_node)); */
528 } while ((temp_node = vconf_keylist_nextnode(keylist)));