4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
30 unsigned requested: 1;
34 static const char prefix_of_permissions[] = FWK_PREFIX_PERMISSION;
36 static int nrpermissions = 0;
37 static struct permission *permissions = NULL;
38 static int indexiter = 0;
40 /* check is the name has the correct prefix for permissions */
41 int is_standard_permission(const char *name)
43 return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
46 /* retrieves the permission of name */
47 static struct permission *get_permission(const char *name)
51 for (i = 0 ; i < nrpermissions ; i++)
52 if (0 == strcmp(permissions[i].name, name))
57 /* add a permission of name */
58 static struct permission *add_permission(const char *name)
60 struct permission *p = get_permission(name);
62 p = realloc(permissions, ((nrpermissions + 8) & ~7) * sizeof(*p));
65 p = permissions + nrpermissions;
66 memset(p, 0, sizeof(*p));
67 p->name = strdup(name);
75 /* remove any granting */
76 void reset_permissions()
79 for (i = 0 ; i < nrpermissions ; i++)
80 permissions[i].granted = 0;
83 /* remove any granting */
84 void crop_permissions(unsigned level)
87 for (i = 0 ; i < nrpermissions ; i++)
88 if (permissions[i].level < level)
89 permissions[i].granted = 0;
92 /* add permissions granted for installation */
93 void grant_permission_list(const char *list)
98 static const char separators[] = " \t\n\r,";
100 iter = strdupa(list);
101 iter += strspn(iter, separators);
103 n = strcspn(iter, separators);
106 p = add_permission(iter);
108 ERROR("Can't allocate permission");
114 iter += strspn(iter, separators);
118 /* checks if the permission 'name' is recorded */
119 int permission_exists(const char *name)
121 return !!get_permission(name);
124 /* request the permission, returns 1 if granted or 0 otherwise */
125 int request_permission(const char *name)
127 struct permission *p = get_permission(name);
136 /* iteration over granted and requested permissions */
137 const char *first_usable_permission()
140 return next_usable_permission();
143 const char *next_usable_permission()
145 while(indexiter < nrpermissions) {
146 struct permission *p = &permissions[indexiter++];
147 if (p->granted && p->requested)