2b89db6287d9e80f3ececf9c70863c90e099230d
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalk_tizen_user.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <errno.h>
12 #include <pwd.h>
13 #include <grp.h>
14
15 int xwalk_tizen_check_user_app(void) {
16   char* buffer = NULL;
17   int err = 0;
18   struct group grp;
19   struct group* current_g;
20   int64_t len = sysconf(_SC_GETGR_R_SIZE_MAX);
21   if (len == -1)
22     len = 1024;
23   buffer = reinterpret_cast<char*>(malloc((size_t)len));
24   if (!buffer)
25     return -EINVAL;
26
27   err = getgrgid_r(getgid(), &grp, buffer, len, &current_g);
28   if (err) {
29   fprintf(stderr, "group can't be determined");
30     fprintf(stderr, "launching an application will not work\n");
31     free(buffer);
32     return -EINVAL;
33   } else {
34     if ( (!current_g) || (
35         strcmp(current_g->gr_name, "users") &&
36         strcmp(current_g->gr_name, "app"))) {
37       fprintf(stderr, "group '%s' is not allowed :",
38             current_g ? current_g->gr_name : "<NULL>");
39       fprintf(stderr, "launching an application will not work\n");
40       free(buffer);
41       return -EINVAL;
42     }
43   }
44   return 0;
45 }