1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** POSIX wrapper for THREADX                                             */
16 /**                                                                       */
17 /**                                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /* Include necessary system files.  */
23 
24 #include "tx_api.h"    /* Threadx API */
25 #include "pthread.h"  /* Posix API */
26 #include "px_int.h"    /* Posix helper functions */
27 
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    sem_unlink                                         PORTABLE C       */
34 /*                                                           6.2.0        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This routine removes the string from the semaphore name table ,and  */
42 /*    marks the corresponding semaphore for destruction.                  */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    name                   Semaphore name.                              */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    OK                     If successful                                */
51 /*    ERROR                  IF fails                                     */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    posix_find_sem         checks the name of semaphore with the names  */
56 /*                            of the already created semaphore.           */
57 /*    posix_sem_reset        Resets the semaphore structure               */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Application Code                                                    */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  06-02-2021      William E. Lamie        Initial Version 6.1.7         */
68 /*  10-31-2022      Scott Larson            Remove double parenthesis,    */
69 /*                                            resulting in version 6.2.0  */
70 /*                                                                        */
71 /**************************************************************************/
sem_unlink(const CHAR * name)72 INT sem_unlink(const CHAR * name)
73 {
74 
75 
76 struct POSIX_SEMAPHORE_STRUCT          * sem;
77 ULONG                                    len;
78 
79 
80     /* Checking for the invalid length.  */
81     len = strlen(name);
82 
83     if(len > SEM_NAME_MAX)
84     {
85         /* Return appropriate error.  */
86         posix_errno=ENAMETOOLONG;
87         posix_set_pthread_errno(ENAMETOOLONG);
88         /* Return error.  */
89         return(ERROR);
90     }
91 
92     if(!(sem=posix_find_sem(name)))
93     {
94         /* Set error in global variable.  */
95         posix_errno = ENOENT;
96 	    posix_set_pthread_errno(ENOENT);
97 
98         /* Return Error.  */
99         return(ERROR);
100     }
101     if(sem)
102 
103         sem->unlink_flag =TX_TRUE;
104 
105     /* Check for the count.  */
106     if(sem->count == 0)
107     {
108         posix_sem_reset(sem );
109         sem = NULL;
110     }
111 
112     return(OK);
113 }
114