fixed build on 64 bit systems
[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
38 #define BUFSIZE         1024
39
40 const int SHARED_PERM = 0666;
41 const int USER_PERM = 0644;
42
43 const char *HEYNOTI_PREFIX = "/opt/share/noti";
44
45 static int is_app = FALSE;
46 static int perm = 0;
47 static int user_id = 5000;
48
49
50 static GOptionEntry entries[] = {
51         {"application", 'a', 0, G_OPTION_ARG_NONE, &is_app,
52         "allow for application", NULL},
53         {NULL}
54 };
55
56
57 static void __print_help(const char *cmd)
58 {
59         fprintf(stderr, "Usage:\n");
60         fprintf(stderr, "\n");
61         fprintf(stderr, "[Set heynoti key]\n");
62         fprintf(stderr,
63                 "       %s set <KEY NAME>\n", cmd);
64         fprintf(stderr, "\n");
65         fprintf(stderr, "       Ex) %s set heynoti_test\n", cmd);
66         fprintf(stderr, "\n");
67         fprintf(stderr, "       <OPTIONS>\n");
68         fprintf(stderr,
69                 "          -a : Allow application to publish the key.\n");
70         fprintf(stderr, "\n");
71         fprintf(stderr, "       Ex) %s set heynoti_test2 -a\n", cmd);
72         fprintf(stderr, "\n");
73 //      fprintf(stderr, "       %s unset <KEY NAME>\n", cmd);
74 //      fprintf(stderr, "\n");
75 //      fprintf(stderr, "       Ex) %s unset heynoti_test\n", cmd);
76 //      fprintf(stderr, "\n");
77 }
78
79 static int __make_file_path(char *pszKey, char *pszBuf)
80 {
81         snprintf(pszBuf, BUFSIZE, "%s/%s", HEYNOTI_PREFIX, pszKey);
82         return 0;
83 }
84
85 int main(int argc, char **argv)
86 {
87         char szFilePath[BUFSIZE] = { 0, };
88         int fd;
89
90         GError *error = NULL;
91         GOptionContext *context;
92
93         g_type_init();
94         context = g_option_context_new("- vconf library tool");
95         g_option_context_add_main_entries(context, entries, NULL);
96         g_option_context_set_help_enabled(context, FALSE);
97         g_option_context_set_ignore_unknown_options(context, TRUE);
98
99         if (!g_option_context_parse(context, &argc, &argv, &error)) {
100                 g_print("option parsing failed: %s\n", error->message);
101                 exit(1);
102         }
103
104         if (argc < 2) {
105                 __print_help(argv[0]);
106                 return 1;
107         }
108
109         if (!strncmp(argv[1], "set", 3)) {
110                 if (argc < 3) {
111                         __print_help(argv[0]);
112                         return 1;
113                 }
114
115
116                 /*  Start DAC  *************************************/
117                 if (0 != getuid()) {
118                         fprintf(stderr,
119                                 "Error!\t Only root user can use '-g or -u' option\n");
120                         return -1;
121                 }
122
123                 if (__make_file_path(argv[2], szFilePath)) {
124                         fprintf(stderr, "Error!\t Bad prefix\n");
125                         return -1;
126                 }
127                 /*  End DAC  ***************************************/
128
129
130                 /*  Start File creation ********************************/
131                 if(is_app)
132                         perm = SHARED_PERM;
133                 else
134                         perm = USER_PERM;
135
136                 if ((fd = open(szFilePath, O_RDONLY)) == -1) {
137                         if ((fd = open(szFilePath, O_CREAT, 0644)) < 0) {
138                                 return -1;
139                         }
140                         fchmod(fd, perm);
141                         close(fd);
142                 } else {
143                         fchmod(fd, perm);
144                         close(fd);
145                 }
146                 /*  End File creation **********************************/
147
148         } else if (!strncmp(argv[1], "unset", 5)) {
149                 if (argv[2]) {
150                         if (__make_file_path(argv[2], szFilePath)) {
151                                 fprintf(stderr, "Error!\t Bad key string\n");
152                                 return -1;
153                         }
154
155                         if (remove(szFilePath)) {
156                                 fprintf(stderr, "Error!\t fail to remove file\n");
157                                 return -1;
158                         }
159                 }
160                 else
161                         __print_help(argv[0]);
162         } else
163                 fprintf(stderr, "%s is a invalid command\n", argv[1]);
164         return 0;
165 }
166