1 /*
2 * Copyright (c) 2025 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <fcntl.h>
31 #include <semaphore.h>
32 #include <sys/posix_sem.h>
33 #include <string.h>
34 #include <stdio.h>
35
36 /* spawned helper binary, so we don't have darwintest here */
37 /* usage: posix_sem_namespace_helper_teamN <semaphore_name> <operation> */
38 int
main(int argc,char * argv[])39 main(int argc, char *argv[])
40 {
41 if (argc != 3) {
42 fprintf(stderr, "error: wrong number of arguments (%d)\n", argc);
43 return -1;
44 }
45
46 assert(argv[0] != NULL && strlen(argv[0]) > 0);
47 int team_id = argv[0][strlen(argv[0]) - 1] - '0';
48 if (team_id != 0 && team_id != 1) {
49 fprintf(stderr, "error: invalid team_id %d\n", team_id);
50 return -1;
51 }
52
53 char *sem_name = argv[1];
54 char *op = argv[2];
55
56 printf("running %s (%s)\n", op, sem_name);
57 fflush(stdout);
58
59 if (!strcmp(op, "open_excl")) {
60 if (sem_open(sem_name, O_CREAT | O_EXCL, 0755, 0) == SEM_FAILED) {
61 fprintf(stderr, "%s: ", sem_name);
62 perror("sem_open (create exclusive)");
63 return -1;
64 }
65 } else if (!strcmp(op, "check_access")) {
66 if (sem_open(sem_name, 0) == SEM_FAILED) {
67 fprintf(stderr, "%s: ", sem_name);
68 perror("sem_open (check_access)");
69 return -1;
70 }
71 } else if (!strcmp(op, "check_no_access")) {
72 if (sem_open(sem_name, 0) != SEM_FAILED) {
73 fprintf(stderr, "%s: sem_open unexpectedly succeeded\n", sem_name);
74 return -1;
75 }
76 } else if (!strcmp(op, "unlink")) {
77 if (sem_unlink(sem_name) != 0) {
78 fprintf(stderr, "%s: ", sem_name);
79 perror("sem_unlink");
80 return -1;
81 }
82 } else if (!strcmp(op, "unlink_force")) {
83 sem_unlink(sem_name);
84 }
85 }
86