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 #include "tx_thread.h"  /* Internal ThreadX thread management.  */
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    signal                                              PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function assigns a function ("signal handler") pointed to by   */
42 /*    func to be invoked when signal signo occurs.                        */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    set                            Pointer to set of signals            */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    OK                        If successful                             */
51 /*    ERROR                     If error occurs                           */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    posix_in_thread_context                                             */
56 /*    posix_internal_error                                                */
57 /*    posix_set_pthread_errno                                             */
58 /*    tx_thread_identify                                                  */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application Code                                                    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
69 /*                                                                        */
70 /**************************************************************************/
signal(int signo,void (* func)(int))71 int  signal(int signo, void (*func)(int))
72 {
73 
74 POSIX_TCB   *current_thread;
75 
76 
77     /* Make sure we're calling this routine from a thread context.  */
78     if (!posix_in_thread_context())
79     {
80         /* return POSIX error.  */
81         posix_internal_error(444);
82         return(444);
83     }
84 
85     /* Determine if the desired signal is valid.  */
86     if ((signo < 0) || (signo > SIGRTMAX))
87     {
88 
89         /* Return an error.  */
90         posix_set_pthread_errno(EINVAL);
91         return(ERROR);
92     }
93 
94     /* Now pickup the current thread pointer.  */
95     current_thread =  (POSIX_TCB *) tx_thread_identify();
96 
97     /* Now index into the array of signal handlers and insert this one.  */
98     current_thread -> signals.signal_func[signo] =  func;
99 
100     /* Return success!  */
101     return(OK);
102 }
103