1 /* Helper code for POSIX semaphore implementation.
2 Copyright (C) 2021-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 #include <semaphoreP.h>
22 #include <sem_routines.h>
24 /* Keeping track of currently used mappings. */
40 char name
[NAME_MAX
+ 1];
43 /* Comparison function for search of existing mapping. */
45 sem_search (const void *a
, const void *b
)
47 const struct inuse_sem
*as
= (const struct inuse_sem
*) a
;
48 const struct inuse_sem
*bs
= (const struct inuse_sem
*) b
;
50 if (as
->ino
!= bs
->ino
)
51 /* Cannot return the difference the type is larger than int. */
52 return as
->ino
< bs
->ino
? -1 : (as
->ino
== bs
->ino
? 0 : 1);
54 if (as
->dev
!= bs
->dev
)
55 /* Cannot return the difference the type is larger than int. */
56 return as
->dev
< bs
->dev
? -1 : (as
->dev
== bs
->dev
? 0 : 1);
58 return strcmp (as
->name
, bs
->name
);
61 /* The search tree for existing mappings. */
62 static void *sem_mappings
;
64 /* Lock to protect the search tree. */
65 static int sem_mappings_lock
= LLL_LOCK_INITIALIZER
;
68 /* Search for existing mapping and if possible add the one provided. */
70 __sem_check_add_mapping (const char *name
, int fd
, sem_t
*existing
)
72 size_t namelen
= strlen (name
);
73 if (namelen
> NAME_MAX
)
77 sem_t
*result
= SEM_FAILED
;
79 /* Get the information about the file. */
80 struct __stat64_t64 st
;
81 if (__fstat64_time64 (fd
, &st
) == 0)
84 lll_lock (sem_mappings_lock
, LLL_PRIVATE
);
86 /* Search for an existing mapping given the information we have. */
87 struct search_sem fake
;
88 memcpy (fake
.name
, name
, namelen
);
92 struct inuse_sem
**foundp
= __tfind (&fake
, &sem_mappings
, sem_search
);
95 /* There is already a mapping. Use it. */
96 result
= (*foundp
)->sem
;
101 /* We haven't found a mapping. Install ione. */
102 struct inuse_sem
*newp
;
104 newp
= (struct inuse_sem
*) malloc (sizeof (*newp
) + namelen
);
107 /* If the caller hasn't provided any map it now. */
108 if (existing
== SEM_FAILED
)
109 existing
= (sem_t
*) __mmap (NULL
, sizeof (sem_t
),
110 PROT_READ
| PROT_WRITE
,
113 newp
->dev
= st
.st_dev
;
114 newp
->ino
= st
.st_ino
;
116 newp
->sem
= existing
;
117 memcpy (newp
->name
, name
, namelen
);
119 /* Insert the new value. */
120 if (existing
!= MAP_FAILED
121 && __tsearch (newp
, &sem_mappings
, sem_search
) != NULL
)
125 /* Something went wrong while inserting the new
126 value. We fail completely. */
131 /* Release the lock. */
132 lll_unlock (sem_mappings_lock
, LLL_PRIVATE
);
135 if (result
!= existing
&& existing
!= SEM_FAILED
&& existing
!= MAP_FAILED
)
137 /* Do not disturb errno. */
139 __munmap (existing
, sizeof (sem_t
));
149 struct inuse_sem
*rec
;
153 walker (const void *inodep
, VISIT which
, void *closure0
)
155 struct walk_closure
*closure
= closure0
;
156 struct inuse_sem
*nodep
= *(struct inuse_sem
**) inodep
;
158 if (nodep
->sem
== closure
->the_sem
)
159 closure
->rec
= nodep
;
163 __sem_remove_mapping (sem_t
*sem
)
168 lll_lock (sem_mappings_lock
, LLL_PRIVATE
);
170 /* Locate the entry for the mapping the caller provided. */
171 struct inuse_sem
*rec
;
173 struct walk_closure closure
= { .the_sem
= sem
, .rec
= NULL
};
174 __twalk_r (sem_mappings
, walker
, &closure
);
179 /* Check the reference counter. If it is going to be zero, free
180 all the resources. */
181 if (--rec
->refcnt
== 0)
183 /* Remove the record from the tree. */
184 __tdelete (rec
, &sem_mappings
, sem_search
);
186 if (__munmap (rec
->sem
, sizeof (sem_t
)) == -1)
195 /* Release the lock. */
196 lll_unlock (sem_mappings_lock
, LLL_PRIVATE
);