]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/wrappers/persona.c
xnu-4903.241.1.tar.gz
[apple/xnu.git] / libsyscall / wrappers / persona.c
CommitLineData
490019cf
A
1/*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#include <sys/cdefs.h>
24#include <sys/types.h>
25#include <stdarg.h>
26#include <stdint.h>
27#include <sys/persona.h>
28
29#include "strings.h"
30
31/* syscall entry point */
32int __persona(uint32_t operation, uint32_t flags, struct kpersona_info *info, uid_t *id, size_t *idlen);
33
34int kpersona_alloc(struct kpersona_info *info, uid_t *id)
35{
36 size_t idlen = 1;
37 return __persona(PERSONA_OP_ALLOC, 0, info, id, &idlen);
38}
39
40int kpersona_dealloc(uid_t id)
41{
42 size_t idlen = 1;
43 return __persona(PERSONA_OP_DEALLOC, 0, NULL, &id, &idlen);
44}
45
46int kpersona_get(uid_t *id)
47{
48 /* persona is a process-static identifier: cache it in a global */
49 static uid_t p_id = PERSONA_ID_NONE;
50 if (p_id == PERSONA_ID_NONE) {
51 int ret = 0;
52 size_t idlen = 1;
53 ret = __persona(PERSONA_OP_GET, 0, NULL, &p_id, &idlen);
54 if (ret != 0)
55 return ret;
56 }
57 *id = p_id;
58 return 0;
59}
60
61int kpersona_info(uid_t id, struct kpersona_info *info)
62{
63 size_t idlen = 1;
64 return __persona(PERSONA_OP_INFO, 0, info, &id, &idlen);
65}
66
67int kpersona_pidinfo(pid_t pid, struct kpersona_info *info)
68{
69 size_t idlen = 1;
70 uid_t id = (uid_t)pid;
71 return __persona(PERSONA_OP_PIDINFO, 0, info, &id, &idlen);
72}
73
74int kpersona_find(const char *name, uid_t uid, uid_t *id, size_t *idlen)
75{
76 int ret;
77 struct kpersona_info kinfo;
78 kinfo.persona_info_version = PERSONA_INFO_V1;
79 kinfo.persona_id = uid;
80 kinfo.persona_type = 0;
81 kinfo.persona_gid = 0;
82 kinfo.persona_ngroups = 0;
83 kinfo.persona_groups[0] = 0;
84 kinfo.persona_name[0] = 0;
85 if (name)
86 strlcpy(kinfo.persona_name, name, sizeof(kinfo.persona_name));
87 ret = __persona(PERSONA_OP_FIND, 0, &kinfo, id, idlen);
88 if (ret < 0)
89 return ret;
90 return (int)(*idlen);
91}