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>
40 VCONFTOOL_TYPE_NO = 0x00,
41 VCONFTOOL_TYPE_STRING,
43 VCONFTOOL_TYPE_DOUBLE,
49 const char *BACKEND_DB_PREFIX = "db/";
50 const char *BACKEND_FILE_PREFIX = "file/";
51 const char *BACKEND_MEMORY_PREFIX = "memory/";
53 const char *DB_PREFIX = "/opt/var/kdb";
54 const char *FILE_PREFIX = "/opt/var/kdb";
55 const char *MEMORY_PREFIX = "/var/run";
56 const char *MEMORY_INIT = "/opt/var/kdb/memory_init";
58 static char *guid = NULL;
59 static char *uid = NULL;
60 static char *vconf_type = NULL;
61 static int is_recursive = FALSE;
62 static int is_initialization = FALSE;
63 static int is_forced = FALSE;
64 static int get_num = 0;
66 static GOptionEntry entries[] = {
67 {"type", 't', 0, G_OPTION_ARG_STRING, &vconf_type, "type of value",
68 "int|bool|double|string"},
69 {"recursive", 'r', 0, G_OPTION_ARG_NONE, &is_recursive,
70 "retrieve keys recursively", NULL},
71 {"guid", 'g', 0, G_OPTION_ARG_STRING, &guid, "group permission", NULL},
72 {"uid", 'u', 0, G_OPTION_ARG_STRING, &uid, "user permission", NULL},
73 {"initialization", 'i', 0, G_OPTION_ARG_NONE, &is_initialization,
74 "memory backend initialization", NULL},
75 {"force", 'f', 0, G_OPTION_ARG_NONE, &is_forced,
76 "overwrite vconf values by force", NULL},
80 static void get_operation(char *input);
81 static void recursive_get(char *subDIR, int level);
82 static void print_keylist(keylist_t *keylist, keynode_t *temp_node, int level);
84 static void print_help(const char *cmd)
86 fprintf(stderr, "Usage:\n");
87 fprintf(stderr, "\n");
88 fprintf(stderr, "[Set vconf value]\n");
90 " %s set -t <TYPE> <KEY NAME> <VALUE> <OPTIONS>\n", cmd);
91 fprintf(stderr, " <TYPE>=int|bool|double|string\n");
92 fprintf(stderr, "\n");
94 " Ex) %s set -t string db/testapp/key1 \"This is test\" \n",
96 fprintf(stderr, "\n");
97 fprintf(stderr, " <OPTIONS>\n");
99 " -g <GUID> : Set Effective group id. The key's permission will be set to 0664.\n");
101 " Ex) %s set -t string db/testapp/key1 \"This is test\" -g 425\n",
104 " NOTE: -g and -u cannot be userd together. -u ignores -g option.\n");
105 fprintf(stderr, "\n");
107 " -u <UID> : Set Effective user id. The key's permission will be set to 0644.\n");
109 " Ex) %s set -t string db/testapp/key1 \"This is test\" -u 5000\n",
112 " NOTE: -g and -u cannot be userd together. -u ignores -g option.\n");
113 fprintf(stderr, "\n");
115 " -i : Install memory backend key into flash space for backup.\n");
117 " Ex) %s set -t string memory/testapp/key1 \"This is test\" -i\n",
119 fprintf(stderr, "\n");
121 " -f : Overwrite values by force, even when vconf values are already exist.\n");
122 fprintf(stderr, "\n");
123 fprintf(stderr, "[Get vconf value]\n");
124 fprintf(stderr, " %s get <OPTIONS> <KEY NAME>\n", cmd);
125 fprintf(stderr, "\n");
126 fprintf(stderr, " <OPTIONS>\n");
128 " -r : retrieve all keys included in sub-directorys \n");
129 fprintf(stderr, " Ex) %s get db/testapp/key1\n", cmd);
130 fprintf(stderr, " %s get db/testapp/\n", cmd);
131 fprintf(stderr, "\n");
132 fprintf(stderr, "[Unset vconf value]\n");
133 fprintf(stderr, " %s unset <KEY NAME>\n", cmd);
134 fprintf(stderr, "\n");
135 fprintf(stderr, " Ex) %s unset db/testapp/key1\n", cmd);
136 fprintf(stderr, "\n");
139 static int check_type(void)
142 if (!strncasecmp(vconf_type, "int", 3))
143 return VCONFTOOL_TYPE_INT;
144 else if (!strncasecmp(vconf_type, "string", 6))
145 return VCONFTOOL_TYPE_STRING;
146 else if (!strncasecmp(vconf_type, "double", 6))
147 return VCONFTOOL_TYPE_DOUBLE;
148 else if (!strncasecmp(vconf_type, "bool", 4))
149 return VCONFTOOL_TYPE_BOOL;
151 return VCONFTOOL_TYPE_NO;
154 static int __system(char * cmd)
159 if((cpid = fork()) < 0) {
172 execv(w[0], (char *const *)w);
179 if (waitpid(cpid, &status, 0) == -1) {
180 perror("waitpid failed");
183 if (WIFSIGNALED(status)) {
184 printf("signal(%d)\n", WTERMSIG(status));
185 perror("exit by signal");
188 if (!WIFEXITED(status)) {
189 perror("exit abnormally");
192 if (WIFEXITED(status) && WEXITSTATUS(status)) {
193 perror("child return error");
201 static void disable_invalid_char(char* src)
205 for(tmp = src; *tmp; ++tmp)
207 if( (*tmp == ';') || (*tmp == '|') )
209 fprintf(stderr,"invalid char is found\n");
215 static int check_file_path_mode(char* file_path)
217 char szCmd[BUFSIZE] = {0,};
225 "Error!\t Only root user can use '-g or -u' option\n");
232 /* Check file path */
233 if (access(file_path, F_OK) != 0) {
234 /* fprintf(stderr,"key does not exist\n"); */
236 char szPath[BUFSIZE] = { 0, };
237 char *pCh = strrchr(file_path, '/');
238 strncat(szPath, file_path, pCh - file_path);
239 /* fprintf(stderr, "szPath : %s\n", szPath); */
241 /* Check directory & create it */
242 if (access(szPath, F_OK) != 0) {
243 /* fprintf(stderr,"parent dir does not exist\n"); */
245 snprintf(szCmd, BUFSIZE, "/bin/mkdir %s -p --mode=755", szPath);
246 disable_invalid_char(szCmd);
247 if (__system(szCmd)) {
248 fprintf(stderr,"[%s:%d]Fail mkdir() szCmd=%s\n", __FILE__, __LINE__, szCmd);
255 } else if (!is_forced) {
256 fprintf(stderr, "Key already exist. Use -f option to force update\n");
264 fd = open(file_path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
267 fprintf(stderr, "open(rdwr,create) error\n");
274 if((fd = open(file_path, O_RDONLY)) == -1) {
275 fprintf(stderr, "open(rdonly) error\n");
280 if (fchown(fd, atoi(uid), atoi(uid)) == -1) {
281 fprintf(stderr, "Error!\t Fail to fchown(%d)\n", errno);
286 if (-1 == fchown(fd, 0, atoi(guid))) {
287 fprintf(stderr, "Error!\t Fail to fchown()\n");
300 * there are three different types of vconf key back-end
302 * 2. sqlitefs based on sqlite3
303 * 3. tmpfs based on memory
304 * considering to that tmpfs is volatile so there should be a way
305 * to initialize key into permanent storage. That is the below function
306 * which copy memory back-end key to filesystem back-end, copy other back-end
307 * key to filesystem back-end just makes no sense.
309 static int copy_memory_key(char *pszKey, char *pszOrigin)
311 char szCmd[BUFSIZE] = { 0, };
312 char szPath[BUFSIZE] = { 0, };
313 char szFileName[BUFSIZE] = { 0, };
314 char *pCh = strrchr(pszKey, '/');
315 int nLen = strlen(pszKey);
317 /* only copy memory/ prefix key */
318 if (strncmp("memory/", pszKey, 7))
321 /* Get directory path and file name */
322 snprintf(szPath, BUFSIZE, "%s/", MEMORY_INIT);
323 strncat(szPath, pszKey, pCh - pszKey);
324 strncpy(szFileName, pszKey + (pCh - pszKey) + 1, nLen - (pCh - pszKey));
326 /* Check directory & create it */
327 if (0 != access(szPath, F_OK)) {
328 snprintf(szCmd, BUFSIZE, "mkdir %s -p --mode=755", szPath);
330 printf("[%s:%d]Fail mkdir() szCmd=%s\n", __FILE__,
336 strncat(szPath, "/", 1);
337 strncat(szPath, szFileName, strlen(szFileName));
338 memset(szCmd, 0x00, BUFSIZE);
339 snprintf(szCmd, BUFSIZE, "cp %s %s -r -p", pszOrigin, szPath);
341 printf("[%s:%d]Fail copy\n", __FILE__, __LINE__);
347 static int make_file_path(char *pszKey, char *pszBuf)
350 strncmp(pszKey, BACKEND_DB_PREFIX, strlen(BACKEND_DB_PREFIX))) {
351 snprintf(pszBuf, BUFSIZE, "%s/%s", DB_PREFIX, pszKey);
354 strncmp(pszKey, BACKEND_FILE_PREFIX,
355 strlen(BACKEND_FILE_PREFIX))) {
356 snprintf(pszBuf, BUFSIZE, "%s/%s", FILE_PREFIX, pszKey);
359 strncmp(pszKey, BACKEND_MEMORY_PREFIX,
360 strlen(BACKEND_MEMORY_PREFIX))) {
361 snprintf(pszBuf, BUFSIZE, "%s/%s", MEMORY_PREFIX, pszKey);
367 int main(int argc, char **argv)
370 char szFilePath[BUFSIZE] = { 0, };
371 char *psz_key = NULL;
373 GError *error = NULL;
374 GOptionContext *context;
377 context = g_option_context_new("- vconf library tool");
378 g_option_context_add_main_entries(context, entries, NULL);
379 g_option_context_set_help_enabled(context, FALSE);
380 g_option_context_set_ignore_unknown_options(context, TRUE);
382 if (!g_option_context_parse(context, &argc, &argv, &error)) {
383 g_print("option parsing failed: %s\n", error->message);
392 if (!strncmp(argv[1], "set", 3)) {
393 set_type = check_type();
394 if (argc < 4 || !set_type) {
399 if (make_file_path(argv[2], szFilePath)) {
400 fprintf(stderr, "Error!\t Bad prefix\n");
404 if (check_file_path_mode(szFilePath)) {
405 fprintf(stderr, "Error!\t create key %s\n", argv[2]);
410 case VCONFTOOL_TYPE_STRING:
411 vconf_set_str(argv[2], argv[3]);
413 case VCONFTOOL_TYPE_INT:
414 vconf_set_int(argv[2], atoi(argv[3]));
416 case VCONFTOOL_TYPE_DOUBLE:
417 vconf_set_dbl(argv[2], atof(argv[3]));
419 case VCONFTOOL_TYPE_BOOL:
420 vconf_set_bool(argv[2], !!atoi(argv[3]));
423 fprintf(stderr, "never reach");
428 /* Install memory backend key into flash space *******/
429 if (is_initialization) {
430 copy_memory_key(psz_key, szFilePath);
432 /* End memory backend key into flash space ***********/
434 } else if (!strncmp(argv[1], "get", 3)) {
436 get_operation(argv[2]);
439 } else if (!strncmp(argv[1], "unset", 5)) {
441 vconf_unset(argv[2]);
445 fprintf(stderr, "%s is a invalid command\n", argv[1]);
449 static void get_operation(char *input)
451 keylist_t *get_keylist;
452 keynode_t *temp_node;
455 get_keylist = vconf_keylist_new();
456 /* ParentDIR parameter of gconf_client_all_entries
457 can not include the last slash. */
458 if ('/' == input[strlen(input) - 1] && strlen(input) > 8)
459 input[strlen(input) - 1] = '\0';
461 vconf_get(get_keylist, input, VCONF_GET_ALL);
462 if (!(temp_node = vconf_keylist_nextnode(get_keylist))) {
463 test = strrchr(input, '/');
465 vconf_keylist_add_null(get_keylist, input);
466 if (test - input < 7)
470 vconf_get(get_keylist, input, VCONF_GET_KEY);
471 temp_node = vconf_keylist_nextnode(get_keylist);
473 fprintf(stderr, "Include at least one slash\"/\"\n");
474 vconf_keylist_free(get_keylist);
479 print_keylist(get_keylist, temp_node, 0);
483 vconf_keylist_free(get_keylist);
486 static void recursive_get(char *subDIR, int level)
488 printf("%s", subDIR);
490 keylist_t *get_keylist;
491 keynode_t *first_node;
493 get_keylist = vconf_keylist_new();
494 vconf_get(get_keylist, subDIR, VCONF_GET_ALL);
496 if ((first_node = vconf_keylist_nextnode(get_keylist))) {
497 print_keylist(get_keylist, first_node, level);
499 vconf_keylist_free(get_keylist);
502 static void print_keylist(keylist_t *keylist, keynode_t *temp_node, int level)
505 switch (vconf_keynode_get_type(temp_node))
508 printf("%s, value = %d (int)\n",
509 vconf_keynode_get_name(temp_node),
510 vconf_keynode_get_int(temp_node));
513 case VCONF_TYPE_BOOL:
514 printf("%s, value = %d (bool)\n",
515 vconf_keynode_get_name(temp_node),
516 vconf_keynode_get_bool(temp_node));
519 case VCONF_TYPE_DOUBLE:
520 printf("%s, value = %f (double)\n",
521 vconf_keynode_get_name(temp_node),
522 vconf_keynode_get_dbl(temp_node));
525 case VCONF_TYPE_STRING:
526 printf("%s, value = %s (string)\n",
527 vconf_keynode_get_name(temp_node),
528 vconf_keynode_get_str(temp_node));
532 printf("%s(Directory)\n",
533 vconf_keynode_get_name(temp_node));
535 recursive_get(vconf_keynode_get_name(temp_node),
539 /* fprintf(stderr, "Unknown Type(%d)\n", vconf_keynode_get_type(temp_node)); */
542 } while ((temp_node = vconf_keylist_nextnode(keylist)));