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 /*    mq_close                                            PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    close a message queue                                               */
42 /*                                                                        */
43 /*  INPUT                                                                 */
44 /*                                                                        */
45 /*    mqdes                         message queue descriptor              */
46 /*                                                                        */
47 /*  OUTPUT                                                                */
48 /*                                                                        */
49 /*    OK                            If successful                         */
50 /*    ERROR                         If error occurs                       */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*    posix_internal_error          Generic error handler                 */
55 /*    tx_byte_release               Release bytes                         */
56 /*    tx_thread_identify            Returns currently running thread      */
57 /*    posix_queue_delete            Deletes specific queue                */
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 /*                                                                        */
69 /**************************************************************************/
mq_close(mqd_t mqdes)70 INT  mq_close(mqd_t mqdes)
71 {
72 
73 TX_INTERRUPT_SAVE_AREA
74 
75 TX_QUEUE          * Queue;
76 POSIX_MSG_QUEUE   * q_ptr;
77 
78     /* Assign a temporary variable for clarity.  */
79     Queue = &(mqdes->f_data->queue);
80     q_ptr = (POSIX_MSG_QUEUE * )Queue;
81 
82     /* First, check for an invalid queue pointer.  */
83     if ( (!q_ptr) || ( (q_ptr -> px_queue_id) != PX_QUEUE_ID))
84     {
85         /* Queue pointer is invalid, return appropriate error code.  */
86         posix_errno = EBADF;
87 	    posix_set_pthread_errno(EBADF);
88 
89         /* Return ERROR.  */
90         return(ERROR);
91     }
92 
93     /* If we are not calling this routine from a thread context.  */
94     if (!(tx_thread_identify()))
95     {
96         /* return appropriate error code.  */
97         posix_errno = EBADF;
98 	    posix_set_pthread_errno(EBADF);
99 
100         /* Return ERROR.  */
101         return(ERROR);
102     }
103 
104     /* Free the system resource allocated by open call.  */
105     if( tx_byte_release( ( VOID *) mqdes ))
106     {
107         /* return generic error.  */
108         posix_internal_error(100);
109 
110         /* Return error.  */
111         return(ERROR);
112     }
113 
114     /* Disable interrupts.  */
115     TX_DISABLE
116 
117     /* Decrement ref count.  */
118     if( q_ptr->open_count )
119 
120         q_ptr ->open_count--;
121 
122     /* Destroy the basic Queue is the ref count is zero and
123         it is marked by unlink.  */
124     if( (! q_ptr ->open_count ) && (q_ptr->unlink_flag == TX_TRUE))
125     {
126         /* Free the system resource allocated by open call.  */
127         if( posix_queue_delete( q_ptr ))
128         {
129 
130             /* Restore interrupts.  */
131             TX_RESTORE
132 
133             /* return generic type.  */
134             posix_internal_error(100);
135 
136             /* Return error.  */
137             return(ERROR);
138         }
139     }
140     /* Restore interrupts.  */
141     TX_RESTORE
142 
143     /* Return OK.  */
144     return(OK);
145 }
146