Merge branch 'tizen_2.2' into tizen
[platform/core/appfw/heynoti.git] / heynotitool.c
1 /*
2  * heynoti
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <glib-object.h>
30 #include "heynoti.h"
31
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36
37 /* For multi-user support */
38 #include <tzplatform_config.h>
39
40 #define HEYNOTI_PREFIX tzplatform_mkpath(TZ_SYS_SHARE, "noti")
41
42 #define BUFSIZE         1024
43
44 const int SHARED_PERM = 0666;
45 const int USER_PERM = 0644;
46
47 static int is_app = FALSE;
48 static int perm = 0;
49
50
51 static GOptionEntry entries[] = {
52         {"application", 'a', 0, G_OPTION_ARG_NONE, &is_app,
53         "allow for application", NULL},
54         {NULL}
55 };
56
57
58 static void __print_help(const char *cmd)
59 {
60         fprintf(stderr, "Usage:\n");
61         fprintf(stderr, "\n");
62         fprintf(stderr, "[Set heynoti key]\n");
63         fprintf(stderr,
64                 "       %s set <KEY NAME>\n", cmd);
65         fprintf(stderr, "\n");
66         fprintf(stderr, "       Ex) %s set heynoti_test\n", cmd);
67         fprintf(stderr, "\n");
68         fprintf(stderr, "       <OPTIONS>\n");
69         fprintf(stderr,
70                 "          -a : Allow application to publish the key.\n");
71         fprintf(stderr, "\n");
72         fprintf(stderr, "       Ex) %s set heynoti_test2 -a\n", cmd);
73         fprintf(stderr, "\n");
74 //      fprintf(stderr, "       %s unset <KEY NAME>\n", cmd);
75 //      fprintf(stderr, "\n");
76 //      fprintf(stderr, "       Ex) %s unset heynoti_test\n", cmd);
77 //      fprintf(stderr, "\n");
78 }
79
80 static int __make_file_path(char *pszKey, char *pszBuf)
81 {
82         snprintf(pszBuf, BUFSIZE, "%s/%s", HEYNOTI_PREFIX, pszKey);
83         return 0;
84 }
85
86 int main(int argc, char **argv)
87 {
88         char szFilePath[BUFSIZE] = { 0, };
89         int fd;
90
91         GError *error = NULL;
92         GOptionContext *context;
93
94         g_type_init();
95         context = g_option_context_new("- vconf library tool");
96         g_option_context_add_main_entries(context, entries, NULL);
97         g_option_context_set_help_enabled(context, FALSE);
98         g_option_context_set_ignore_unknown_options(context, TRUE);
99
100         if (!g_option_context_parse(context, &argc, &argv, &error)) {
101                 g_print("option parsing failed: %s\n", error->message);
102                 exit(1);
103         }
104
105         if (argc < 2) {
106                 __print_help(argv[0]);
107                 return 1;
108         }
109
110         if (!strncmp(argv[1], "set", 3)) {
111                 if (argc < 3) {
112                         __print_help(argv[0]);
113                         return 1;
114                 }
115
116
117                 /*  Start DAC  *************************************/
118                 if (0 != getuid()) {
119                         fprintf(stderr,
120                                 "Error!\t Only root user can use '-g or -u' option\n");
121                         return -1;
122                 }
123
124                 if (__make_file_path(argv[2], szFilePath)) {
125                         fprintf(stderr, "Error!\t Bad prefix\n");
126                         return -1;
127                 }
128                 /*  End DAC  ***************************************/
129
130
131                 /*  Start File creation ********************************/
132                 if(is_app)
133                         perm = SHARED_PERM;
134                 else
135                         perm = USER_PERM;
136
137                 if ((fd = open(szFilePath, O_RDONLY)) == -1) {
138                         if ((fd = open(szFilePath, O_CREAT, 0644)) < 0) {
139                                 return -1;
140                         }
141                         fchmod(fd, perm);
142                         close(fd);
143                 } else {
144                         fchmod(fd, perm);
145                         close(fd);
146                 }
147                 /*  End File creation **********************************/
148
149         } else if (!strncmp(argv[1], "unset", 5)) {
150                 if (argv[2]) {
151                         if (__make_file_path(argv[2], szFilePath)) {
152                                 fprintf(stderr, "Error!\t Bad key string\n");
153                                 return -1;
154                         }
155
156                         if (remove(szFilePath)) {
157                                 fprintf(stderr, "Error!\t fail to remove file\n");
158                                 return -1;
159                         }
160                 }
161                 else
162                         __print_help(argv[0]);
163         } else
164                 fprintf(stderr, "%s is a invalid command\n", argv[1]);
165         return 0;
166 }
167