|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/ostest/pthread_sigqueue.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | + |
| 29 | +#include <assert.h> |
| 30 | +#include <errno.h> |
| 31 | +#include <pthread.h> |
| 32 | +#include <semaphore.h> |
| 33 | +#include <signal.h> |
| 34 | +#include <stdbool.h> |
| 35 | +#include <stdio.h> |
| 36 | +#include <unistd.h> |
| 37 | + |
| 38 | +#include "ostest.h" |
| 39 | + |
| 40 | +/**************************************************************************** |
| 41 | + * Pre-processor Definitions |
| 42 | + ****************************************************************************/ |
| 43 | + |
| 44 | +#define TEST_SIGNO SIGUSR1 |
| 45 | +#define TEST_VAL_INT 42 |
| 46 | + |
| 47 | +/**************************************************************************** |
| 48 | + * Private Data |
| 49 | + ****************************************************************************/ |
| 50 | + |
| 51 | +static sem_t g_ready_sem; |
| 52 | +static sem_t g_done_sem; |
| 53 | +static struct sigaction g_oact; |
| 54 | +static bool g_act_installed; |
| 55 | +static volatile bool g_sigreceived; |
| 56 | +static volatile int g_received_signo; |
| 57 | +static volatile int g_received_code; |
| 58 | +static volatile int g_received_val; |
| 59 | + |
| 60 | +/**************************************************************************** |
| 61 | + * Private Functions |
| 62 | + ****************************************************************************/ |
| 63 | + |
| 64 | +/**************************************************************************** |
| 65 | + * Name: sigqueue_handler |
| 66 | + ****************************************************************************/ |
| 67 | + |
| 68 | +static void sigqueue_handler(int signo, FAR siginfo_t *info, |
| 69 | + FAR void *ucontext) |
| 70 | +{ |
| 71 | + if (info != NULL) |
| 72 | + { |
| 73 | + g_received_signo = signo; |
| 74 | + g_received_code = info->si_code; |
| 75 | + g_received_val = info->si_value.sival_int; |
| 76 | + g_sigreceived = true; |
| 77 | + } |
| 78 | + |
| 79 | + sem_post(&g_done_sem); |
| 80 | +} |
| 81 | + |
| 82 | +/**************************************************************************** |
| 83 | + * Name: sigqueue_thread |
| 84 | + ****************************************************************************/ |
| 85 | + |
| 86 | +static FAR void *sigqueue_thread(FAR void *arg) |
| 87 | +{ |
| 88 | + struct sigaction act; |
| 89 | + sigset_t set; |
| 90 | + int status; |
| 91 | + |
| 92 | + /* Setup signal action */ |
| 93 | + |
| 94 | + act.sa_sigaction = sigqueue_handler; |
| 95 | + act.sa_flags = SA_SIGINFO; |
| 96 | + sigemptyset(&act.sa_mask); |
| 97 | + |
| 98 | + status = sigaction(TEST_SIGNO, &act, &g_oact); |
| 99 | + if (status != OK) |
| 100 | + { |
| 101 | + printf("sigqueue_thread: sigaction failed, errno=%d\n", errno); |
| 102 | + ASSERT(false); |
| 103 | + return (FAR void *)ERROR; |
| 104 | + } |
| 105 | + |
| 106 | + g_act_installed = true; |
| 107 | + |
| 108 | + /* Unblock test signal */ |
| 109 | + |
| 110 | + sigemptyset(&set); |
| 111 | + sigaddset(&set, TEST_SIGNO); |
| 112 | + status = pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 113 | + if (status != OK) |
| 114 | + { |
| 115 | + printf("sigqueue_thread: pthread_sigmask failed: %d\n", status); |
| 116 | + ASSERT(false); |
| 117 | + return (FAR void *)ERROR; |
| 118 | + } |
| 119 | + |
| 120 | + /* Notify sender that we are ready */ |
| 121 | + |
| 122 | + sem_post(&g_ready_sem); |
| 123 | + |
| 124 | + /* Wait for signal delivery */ |
| 125 | + |
| 126 | + while (sem_wait(&g_done_sem) < 0) |
| 127 | + { |
| 128 | + if (errno != EINTR) |
| 129 | + { |
| 130 | + printf("sigqueue_thread: sem_wait failed: %d\n", errno); |
| 131 | + ASSERT(false); |
| 132 | + return (FAR void *)ERROR; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return NULL; |
| 137 | +} |
| 138 | + |
| 139 | +/**************************************************************************** |
| 140 | + * Public Functions |
| 141 | + ****************************************************************************/ |
| 142 | + |
| 143 | +/**************************************************************************** |
| 144 | + * Name: pthread_sigqueue_test |
| 145 | + ****************************************************************************/ |
| 146 | + |
| 147 | +void pthread_sigqueue_test(void) |
| 148 | +{ |
| 149 | + pthread_t thread; |
| 150 | + union sigval val; |
| 151 | + int status; |
| 152 | + |
| 153 | + sem_init(&g_ready_sem, 0, 0); |
| 154 | + sem_init(&g_done_sem, 0, 0); |
| 155 | + |
| 156 | + g_sigreceived = false; |
| 157 | + g_received_signo = -1; |
| 158 | + g_received_code = -1; |
| 159 | + g_received_val = -1; |
| 160 | + g_act_installed = false; |
| 161 | + |
| 162 | + /* Test 1: Invalid signal number error handling */ |
| 163 | + |
| 164 | + val.sival_int = TEST_VAL_INT; |
| 165 | + status = pthread_sigqueue(pthread_self(), -1, val); |
| 166 | + if (status != EINVAL) |
| 167 | + { |
| 168 | + printf("pthread_sigqueue_test: signo err: %d\n", status); |
| 169 | + ASSERT(false); |
| 170 | + } |
| 171 | + |
| 172 | + /* Test 2: Invalid thread ID error handling */ |
| 173 | + |
| 174 | + status = pthread_sigqueue((pthread_t)-1, TEST_SIGNO, val); |
| 175 | + if (status != ESRCH) |
| 176 | + { |
| 177 | + printf("pthread_sigqueue_test: thread err: %d\n", status); |
| 178 | + ASSERT(false); |
| 179 | + } |
| 180 | + |
| 181 | + /* Test 3: Normal signal queuing to thread */ |
| 182 | + |
| 183 | + status = pthread_create(&thread, NULL, sigqueue_thread, NULL); |
| 184 | + if (status != OK) |
| 185 | + { |
| 186 | + printf("pthread_sigqueue_test: pthread_create: %d\n", status); |
| 187 | + ASSERT(false); |
| 188 | + goto errout; |
| 189 | + } |
| 190 | + |
| 191 | + /* Wait until target thread has installed the handler and unblocked */ |
| 192 | + |
| 193 | + while (sem_wait(&g_ready_sem) < 0) |
| 194 | + { |
| 195 | + if (errno != EINTR) |
| 196 | + { |
| 197 | + printf("pthread_sigqueue_test: sem_wait: %d\n", errno); |
| 198 | + ASSERT(false); |
| 199 | + goto errout; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /* Send signal with payload to target thread */ |
| 204 | + |
| 205 | + status = pthread_sigqueue(thread, TEST_SIGNO, val); |
| 206 | + if (status != OK) |
| 207 | + { |
| 208 | + printf("pthread_sigqueue_test: pthread_sigqueue: %d\n", status); |
| 209 | + ASSERT(false); |
| 210 | + goto errout; |
| 211 | + } |
| 212 | + |
| 213 | + /* Join target thread */ |
| 214 | + |
| 215 | + status = pthread_join(thread, NULL); |
| 216 | + if (status != OK) |
| 217 | + { |
| 218 | + printf("pthread_sigqueue_test: pthread_join: %d\n", status); |
| 219 | + ASSERT(false); |
| 220 | + goto errout; |
| 221 | + } |
| 222 | + |
| 223 | + /* Restore signal action */ |
| 224 | + |
| 225 | + if (g_act_installed) |
| 226 | + { |
| 227 | + status = sigaction(TEST_SIGNO, &g_oact, NULL); |
| 228 | + if (status != OK) |
| 229 | + { |
| 230 | + printf("pthread_sigqueue_test: sigaction restore: %d\n", errno); |
| 231 | + ASSERT(false); |
| 232 | + } |
| 233 | + |
| 234 | + g_act_installed = false; |
| 235 | + } |
| 236 | + |
| 237 | + /* Verify received signal properties */ |
| 238 | + |
| 239 | + if (!g_sigreceived) |
| 240 | + { |
| 241 | + printf("pthread_sigqueue_test: signal not received\n"); |
| 242 | + ASSERT(false); |
| 243 | + } |
| 244 | + |
| 245 | + if (g_received_signo != TEST_SIGNO) |
| 246 | + { |
| 247 | + printf("pthread_sigqueue_test: signo mismatch: %d\n", |
| 248 | + g_received_signo); |
| 249 | + ASSERT(false); |
| 250 | + } |
| 251 | + |
| 252 | + if (g_received_code != SI_QUEUE) |
| 253 | + { |
| 254 | + printf("pthread_sigqueue_test: si_code mismatch: %d\n", |
| 255 | + g_received_code); |
| 256 | + ASSERT(false); |
| 257 | + } |
| 258 | + |
| 259 | + if (g_received_val != TEST_VAL_INT) |
| 260 | + { |
| 261 | + printf("pthread_sigqueue_test: val mismatch: %d\n", |
| 262 | + g_received_val); |
| 263 | + ASSERT(false); |
| 264 | + } |
| 265 | + |
| 266 | + printf("pthread_sigqueue_test: SUCCESS\n"); |
| 267 | + |
| 268 | +errout: |
| 269 | + if (g_act_installed) |
| 270 | + { |
| 271 | + sigaction(TEST_SIGNO, &g_oact, NULL); |
| 272 | + g_act_installed = false; |
| 273 | + } |
| 274 | + |
| 275 | + sem_destroy(&g_ready_sem); |
| 276 | + sem_destroy(&g_done_sem); |
| 277 | +} |
0 commit comments