mqueue.c 26.6 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
Z
zhushengle 已提交
3
 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "mqueue.h"
33
#ifdef LOSCFG_FS_VFS
W
wenjun 已提交
34 35 36 37 38 39
#include "fcntl.h"
#include "pthread.h"
#include "map_error.h"
#include "time_posix.h"
#include "los_memory.h"
#include "los_vm_map.h"
M
mamingshuai 已提交
40
#include "los_process_pri.h"
M
mucor 已提交
41
#include "fs/file.h"
W
wenjun 已提交
42 43 44 45 46
#include "user_copy.h"


#define FNONBLOCK   O_NONBLOCK

Z
zhushengle 已提交
47
#ifndef LOSCFG_IPC_CONTAINER
W
wenjun 已提交
48
/* GLOBALS */
L
lnlan 已提交
49
STATIC fd_set g_queueFdSet;
W
wenjun 已提交
50 51
STATIC struct mqarray g_queueTable[LOSCFG_BASE_IPC_QUEUE_LIMIT];
STATIC pthread_mutex_t g_mqueueMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
M
mamingshuai 已提交
52
STATIC struct mqpersonal *g_mqPrivBuf[MAX_MQ_FD];
W
wenjun 已提交
53

Z
zhushengle 已提交
54 55 56 57 58 59
#define IPC_QUEUE_FD_SET g_queueFdSet
#define IPC_QUEUE_TABLE  g_queueTable
#define IPC_QUEUE_MUTEX  g_mqueueMutex
#define IPC_QUEUE_MQ_PRIV_BUF g_mqPrivBuf
#endif

W
wenjun 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/* LOCAL FUNCTIONS */
STATIC INLINE INT32 MqNameCheck(const CHAR *mqName)
{
    if (mqName == NULL) {
        errno = EINVAL;
        return -1;
    }

    if (strlen(mqName) == 0) {
        errno = EINVAL;
        return -1;
    }

    if (strlen(mqName) > (PATH_MAX - 1)) {
        errno = ENAMETOOLONG;
        return -1;
    }
    return 0;
}

STATIC INLINE UINT32 GetMqueueCBByID(UINT32 queueID, LosQueueCB **queueCB)
{
    LosQueueCB *tmpQueueCB = NULL;
    if (queueCB == NULL) {
        errno = EINVAL;
        return LOS_ERRNO_QUEUE_READ_PTR_NULL;
    }
    tmpQueueCB = GET_QUEUE_HANDLE(queueID);
    if ((GET_QUEUE_INDEX(queueID) >= LOSCFG_BASE_IPC_QUEUE_LIMIT) || (tmpQueueCB->queueID != queueID)) {
        return LOS_ERRNO_QUEUE_INVALID;
    }
    *queueCB = tmpQueueCB;

    return LOS_OK;
}

STATIC INLINE struct mqarray *GetMqueueCBByName(const CHAR *name)
{
    UINT32 index;
    UINT32 mylen = strlen(name);

    for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
Z
zhushengle 已提交
102
        if ((IPC_QUEUE_TABLE[index].mq_name == NULL) || (strlen(IPC_QUEUE_TABLE[index].mq_name) != mylen)) {
W
wenjun 已提交
103 104 105
            continue;
        }

Z
zhushengle 已提交
106 107
        if (strncmp(name, (const CHAR *)(IPC_QUEUE_TABLE[index].mq_name), mylen) == 0) {
            return &(IPC_QUEUE_TABLE[index]);
W
wenjun 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        }
    }
    return NULL;
}

STATIC INT32 DoMqueueDelete(struct mqarray *mqueueCB)
{
    UINT32 ret;

    if (mqueueCB->mq_name != NULL) {
        LOS_MemFree(OS_SYS_MEM_ADDR, mqueueCB->mq_name);
        mqueueCB->mq_name = NULL;
    }

    mqueueCB->mqcb = NULL;
M
mamingshuai 已提交
123 124 125 126
    /* When mqueue-list head node needed free ,reset the mode_data */
    mqueueCB->mode_data.data = 0;
    mqueueCB->euid = -1;
    mqueueCB->egid = -1;
127
    mqueueCB->mq_notify.pid = 0;
W
wenjun 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    ret = LOS_QueueDelete(mqueueCB->mq_id);
    switch (ret) {
        case LOS_OK:
            return 0;
        case LOS_ERRNO_QUEUE_NOT_FOUND:
        case LOS_ERRNO_QUEUE_NOT_CREATE:
        case LOS_ERRNO_QUEUE_IN_TSKUSE:
        case LOS_ERRNO_QUEUE_IN_TSKWRITE:
            errno = EAGAIN;
            return -1;
        default:
            errno = EINVAL;
            return -1;
    }
}

STATIC int SaveMqueueName(const CHAR *mqName, struct mqarray *mqueueCB)
{
    size_t nameLen;

    nameLen = strlen(mqName); /* sys_mq_open has checked name and name length */
    mqueueCB->mq_name = (char *)LOS_MemAlloc(OS_SYS_MEM_ADDR, nameLen + 1);
    if (mqueueCB->mq_name == NULL) {
        errno = ENOMEM;
        return LOS_NOK;
    }

    if (strncpy_s(mqueueCB->mq_name, (nameLen + 1), mqName, nameLen) != EOK) {
        LOS_MemFree(OS_SYS_MEM_ADDR, mqueueCB->mq_name);
        mqueueCB->mq_name = NULL;
        errno = EINVAL;
        return LOS_NOK;
    }
    mqueueCB->mq_name[nameLen] = '\0';
    return LOS_OK;
}

M
mamingshuai 已提交
166
STATIC struct mqpersonal *DoMqueueCreate(const struct mq_attr *attr, const CHAR *mqName, INT32 openFlag, UINT32 mode)
W
wenjun 已提交
167 168 169 170 171 172 173 174 175
{
    struct mqarray *mqueueCB = NULL;
    UINT32 mqueueID;

    UINT32 err = LOS_QueueCreate(NULL, attr->mq_maxmsg, &mqueueID, 0, attr->mq_msgsize);
    if (map_errno(err) != ENOERR) {
        goto ERROUT;
    }

Z
zhushengle 已提交
176 177
    if (IPC_QUEUE_TABLE[GET_QUEUE_INDEX(mqueueID)].mqcb == NULL) {
        mqueueCB = &(IPC_QUEUE_TABLE[GET_QUEUE_INDEX(mqueueID)]);
W
wenjun 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        mqueueCB->mq_id = mqueueID;
    }

    if (mqueueCB == NULL) {
        errno = EINVAL;
        goto ERROUT;
    }

    if (SaveMqueueName(mqName, mqueueCB) != LOS_OK) {
        goto ERROUT;
    }

    if (GetMqueueCBByID(mqueueCB->mq_id, &(mqueueCB->mqcb)) != LOS_OK) {
        errno = ENOSPC;
        goto ERROUT;
    }

    mqueueCB->mq_personal = (struct mqpersonal *)LOS_MemAlloc(OS_SYS_MEM_ADDR, sizeof(struct mqpersonal));
    if (mqueueCB->mq_personal == NULL) {
        (VOID)LOS_QueueDelete(mqueueCB->mq_id);
        mqueueCB->mqcb->queueHandle = NULL;
        mqueueCB->mqcb = NULL;
        errno = ENOSPC;
        goto ERROUT;
    }

    mqueueCB->unlinkflag = FALSE;
M
mamingshuai 已提交
205
    mqueueCB->unlink_ref = 0;
W
wenjun 已提交
206 207 208 209
    mqueueCB->mq_personal->mq_status = MQ_USE_MAGIC;
    mqueueCB->mq_personal->mq_next = NULL;
    mqueueCB->mq_personal->mq_posixdes = mqueueCB;
    mqueueCB->mq_personal->mq_flags = (INT32)((UINT32)openFlag | ((UINT32)attr->mq_flags & (UINT32)FNONBLOCK));
M
mamingshuai 已提交
210 211
    mqueueCB->mq_personal->mq_mode = mode;
    mqueueCB->mq_personal->mq_refcount = 0;
212
    mqueueCB->mq_notify.pid = 0;
W
wenjun 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    return mqueueCB->mq_personal;
ERROUT:

    if ((mqueueCB != NULL) && (mqueueCB->mq_name != NULL)) {
        LOS_MemFree(OS_SYS_MEM_ADDR, mqueueCB->mq_name);
        mqueueCB->mq_name = NULL;
    }
    return (struct mqpersonal *)-1;
}

STATIC struct mqpersonal *DoMqueueOpen(struct mqarray *mqueueCB, INT32 openFlag)
{
    struct mqpersonal *privateMqPersonal = NULL;

    /* already have the same name of g_squeuetable */
    if (mqueueCB->unlinkflag == TRUE) {
        errno = EINVAL;
        goto ERROUT;
    }
    /* alloc mqprivate and add to mqarray */
    privateMqPersonal = (struct mqpersonal *)LOS_MemAlloc(OS_SYS_MEM_ADDR, sizeof(struct mqpersonal));
    if (privateMqPersonal == NULL) {
        errno = ENOSPC;
        goto ERROUT;
    }

    privateMqPersonal->mq_next = mqueueCB->mq_personal;
    mqueueCB->mq_personal = privateMqPersonal;

    privateMqPersonal->mq_posixdes = mqueueCB;
    privateMqPersonal->mq_flags = openFlag;
    privateMqPersonal->mq_status = MQ_USE_MAGIC;
M
mamingshuai 已提交
246
    privateMqPersonal->mq_refcount = 0;
W
wenjun 已提交
247 248 249 250 251 252 253

    return privateMqPersonal;

ERROUT:
    return (struct mqpersonal *)-1;
}

L
lnlan 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
STATIC INT32 DoMqueueClose(struct mqpersonal *privateMqPersonal)
{
    struct mqarray *mqueueCB = NULL;
    struct mqpersonal *tmp = NULL;

    mqueueCB = privateMqPersonal->mq_posixdes;
    if (mqueueCB == NULL || mqueueCB->mq_personal == NULL) {
        errno = EBADF;
        return LOS_NOK;
    }

    /* find the personal and remove */
    if (mqueueCB->mq_personal == privateMqPersonal) {
        mqueueCB->mq_personal = privateMqPersonal->mq_next;
    } else {
        for (tmp = mqueueCB->mq_personal; tmp->mq_next != NULL; tmp = tmp->mq_next) {
            if (tmp->mq_next == privateMqPersonal) {
                break;
            }
        }
        if (tmp->mq_next == NULL) {
            errno = EBADF;
            return LOS_NOK;
        }
        tmp->mq_next = privateMqPersonal->mq_next;
    }
    /* flag no use */
    privateMqPersonal->mq_status = 0;

    /* free the personal */
    (VOID)LOS_MemFree(OS_SYS_MEM_ADDR, privateMqPersonal);

    if ((mqueueCB->unlinkflag == TRUE) && (mqueueCB->mq_personal == NULL)) {
        return DoMqueueDelete(mqueueCB);
    }
    return LOS_OK;
}

M
mamingshuai 已提交
292 293 294 295 296 297 298 299 300 301 302
/* Translate a sysFd into privateMqPersonal */
STATIC struct mqpersonal *MqGetPrivDataBuff(mqd_t personal)
{
    INT32 sysFd = (INT32)personal;
    INT32 id = sysFd - MQUEUE_FD_OFFSET;

    /* Filter illegal id */
    if ((id < 0) || (id >= MAX_MQ_FD)) {
        errno = EBADF;
        return NULL;
    }
Z
zhushengle 已提交
303
    return IPC_QUEUE_MQ_PRIV_BUF[id];
M
mamingshuai 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316
}

/**
 * Alloc sysFd, storage mq private data, set using bit.
 *
 * @param maxfdp: Maximum allowed application of mqueue sysFd.
 * @param fdset: Mqueue sysFd bit map.
 * @param privateMqPersonal: Private data.
 * @return the index of the new fd; -1 on error
 */
STATIC INT32 MqAllocSysFd(int maxfdp, struct mqpersonal *privateMqPersonal)
{
    INT32 i;
Z
zhushengle 已提交
317
    fd_set *fdset = &IPC_QUEUE_FD_SET;
M
mamingshuai 已提交
318 319
    for (i = 0; i < maxfdp; i++) {
        /* sysFd: used bit setting, and get the index of swtmrID buffer */
L
lnlan 已提交
320
        if (fdset && !(FD_ISSET(i + MQUEUE_FD_OFFSET, fdset))) {
M
mamingshuai 已提交
321
            FD_SET(i + MQUEUE_FD_OFFSET, fdset);
Z
zhushengle 已提交
322 323
            if (!IPC_QUEUE_MQ_PRIV_BUF[i]) {
                IPC_QUEUE_MQ_PRIV_BUF[i] = privateMqPersonal;
M
mamingshuai 已提交
324 325 326 327 328 329 330
                return i + MQUEUE_FD_OFFSET;
            }
        }
    }
    return -1;
}

L
lnlan 已提交
331
STATIC VOID MqFreeSysFd(mqd_t personal)
M
mamingshuai 已提交
332 333
{
    INT32 sysFd = (INT32)personal;
Z
zhushengle 已提交
334
    fd_set *fdset = &IPC_QUEUE_FD_SET;
M
mamingshuai 已提交
335 336
    if (fdset && FD_ISSET(sysFd, fdset)) {
        FD_CLR(sysFd, fdset);
Z
zhushengle 已提交
337
        IPC_QUEUE_MQ_PRIV_BUF[sysFd - MQUEUE_FD_OFFSET] = NULL;
M
mamingshuai 已提交
338 339 340 341
    }
}

/* Mqueue fd reference count */
L
lnlan 已提交
342
void MqueueRefer(int sysFd)
M
mamingshuai 已提交
343 344 345 346
{
    struct mqarray *mqueueCB = NULL;
    struct mqpersonal *privateMqPersonal = NULL;

Z
zhushengle 已提交
347
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
M
mamingshuai 已提交
348 349 350 351 352 353 354 355 356
    /* Get the personal sysFd and reset personal fd -1 */
    privateMqPersonal = MqGetPrivDataBuff((mqd_t)sysFd);
    if (privateMqPersonal == NULL) {
        goto OUT_UNLOCK;
    }
    mqueueCB = privateMqPersonal->mq_posixdes;
    if (mqueueCB == NULL) {
        goto OUT_UNLOCK;
    }
L
lnlan 已提交
357

M
mamingshuai 已提交
358 359
    privateMqPersonal->mq_refcount++;
OUT_UNLOCK:
Z
zhushengle 已提交
360
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
L
lnlan 已提交
361
    return;
M
mamingshuai 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
}

STATIC INT32 MqTryClose(struct mqpersonal *privateMqPersonal)
{
    struct mqarray *mqueueCB = NULL;
    mqueueCB = privateMqPersonal->mq_posixdes;
    if (mqueueCB == NULL) {
        errno = ENFILE;
        return false;
    }

    if (privateMqPersonal->mq_refcount == 0) {
        return TRUE;
    }
    privateMqPersonal->mq_refcount--;
    return FALSE;
}

/* Set the mode data bit,for consumer's mode comparing. */
STATIC INT32 MqueueModeAnalysisSet(struct mqpersonal *privateMqPersonal)
{
    UINT32 mode;
    UINT32 intSave;
    User *user = NULL;
    struct mqarray *mqueueCB = NULL;

    if ((INT32)(UINTPTR)privateMqPersonal < 0) {
        return -1;
    }
    /* Get mqueueCB of first time creating mqueue */
    mqueueCB = privateMqPersonal->mq_posixdes;
    if (mqueueCB == NULL) {
        errno = ENFILE;
        return -1;
    }

    mode = mqueueCB->mq_personal->mq_mode;
    /* Set mqueue gid uid */
    SCHEDULER_LOCK(intSave);
    user = OsCurrUserGet();
    mqueueCB->euid = user->effUserID;
    mqueueCB->egid = user->effGid;
    SCHEDULER_UNLOCK(intSave);

    /* Set mode data bit */
    if (mode & S_IRUSR) {
        mqueueCB->mode_data.usr |= S_IRUSR;
    }
    if (mode & S_IWUSR) {
        mqueueCB->mode_data.usr |= S_IWUSR;
    }
    if (mode & S_IRGRP) {
        mqueueCB->mode_data.grp |= S_IRGRP;
    }
    if (mode & S_IWGRP) {
        mqueueCB->mode_data.grp |= S_IWGRP;
    }
    if (mode & S_IROTH) {
        mqueueCB->mode_data.oth |= S_IROTH;
    }
    if (mode & S_IWOTH) {
        mqueueCB->mode_data.oth |= S_IWOTH;
    }
    return 0;
}

STATIC INT32 GetPermissionOfVisitor(struct mqarray *mqueueCB)
{
    uid_t euid;
    gid_t egid;
    UINT32 intSave;
    User *user = NULL;

    if (mqueueCB == NULL) {
        errno = ENOENT;
        return -EPERM;
    }

    /* Get the visitor process euid and egid */
    SCHEDULER_LOCK(intSave);
    user = OsCurrUserGet();
    euid = user->effUserID;
    egid = user->effGid;
    SCHEDULER_UNLOCK(intSave);

    /* root */
    if (euid == 0) {
        return ENOERR;
    }
    if (euid == mqueueCB->euid) { /* usr */
        if (!((mqueueCB->mode_data.usr & S_IRUSR) || (mqueueCB->mode_data.usr & S_IWUSR))) {
            errno = EACCES;
            goto ERR_OUT;
        }
    } else if (egid == mqueueCB->egid) { /* grp */
        if (!((mqueueCB->mode_data.grp & S_IRGRP) || (mqueueCB->mode_data.grp & S_IWGRP))) {
            errno = EACCES;
            goto ERR_OUT;
        }
    } else { /* oth */
        if (!((mqueueCB->mode_data.oth & S_IROTH) || (mqueueCB->mode_data.oth & S_IWOTH))) {
            errno = EACCES;
            goto ERR_OUT;
        }
    }
    return ENOERR;

ERR_OUT:
    return -EPERM;
}

STATIC INT32 GetMqueueAttr(struct mq_attr *defaultAttr, struct mq_attr *attr)
{
    if (attr != NULL) {
        if (LOS_ArchCopyFromUser(defaultAttr, attr, sizeof(struct mq_attr))) {
            errno = EFAULT;
            return -1;
        }
        if ((defaultAttr->mq_maxmsg < 0) || (defaultAttr->mq_maxmsg > (long int)USHRT_MAX) ||
            (defaultAttr->mq_msgsize < 0) || (defaultAttr->mq_msgsize > (long int)(USHRT_MAX - sizeof(UINT32)))) {
            errno = EINVAL;
            return -1;
        }
    }
    return 0;
}

W
wenjun 已提交
489 490 491 492 493 494 495
mqd_t mq_open(const char *mqName, int openFlag, ...)
{
    struct mqarray *mqueueCB = NULL;
    struct mqpersonal *privateMqPersonal = (struct mqpersonal *)-1;
    struct mq_attr *attr = NULL;
    struct mq_attr defaultAttr = { 0, MQ_MAX_MSG_NUM, MQ_MAX_MSG_LEN, 0 };
    va_list ap;
M
mamingshuai 已提交
496 497 498
    int sysFd;
    mqd_t mqFd = -1;
    unsigned int mode = 0;
W
wenjun 已提交
499 500 501 502 503

    if (MqNameCheck(mqName) == -1) {
        return (mqd_t)-1;
    }

Z
zhushengle 已提交
504
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
505 506 507 508 509 510 511 512 513 514
    mqueueCB = GetMqueueCBByName(mqName);
    if ((UINT32)openFlag & (UINT32)O_CREAT) {
        if (mqueueCB != NULL) {
            if (((UINT32)openFlag & (UINT32)O_EXCL)) {
                errno = EEXIST;
                goto OUT;
            }
            privateMqPersonal = DoMqueueOpen(mqueueCB, openFlag);
        } else {
            va_start(ap, openFlag);
M
mamingshuai 已提交
515
            mode = va_arg(ap, unsigned int);
W
wenjun 已提交
516 517 518
            attr = va_arg(ap, struct mq_attr *);
            va_end(ap);

M
mamingshuai 已提交
519 520
            if (GetMqueueAttr(&defaultAttr, attr)) {
                goto OUT;
W
wenjun 已提交
521
            }
M
mamingshuai 已提交
522 523 524 525
            privateMqPersonal = DoMqueueCreate(&defaultAttr, mqName, openFlag, mode);
        }
        /* Set mode data bit ,just for the first node */
        if (MqueueModeAnalysisSet(privateMqPersonal)) {
L
lnlan 已提交
526 527 528
            if ((INT32)(UINTPTR)privateMqPersonal > 0) {
                (VOID)DoMqueueClose(privateMqPersonal);
            }
M
mamingshuai 已提交
529
            goto OUT;
W
wenjun 已提交
530 531
        }
    } else {
M
mamingshuai 已提交
532
        if (GetPermissionOfVisitor(mqueueCB)) {
W
wenjun 已提交
533 534 535 536
            goto OUT;
        }
        privateMqPersonal = DoMqueueOpen(mqueueCB, openFlag);
    }
L
lnlan 已提交
537

M
mamingshuai 已提交
538 539 540 541
    if ((INT32)(UINTPTR)privateMqPersonal > 0) {
        /* alloc sysFd */
        sysFd = MqAllocSysFd(MAX_MQ_FD, privateMqPersonal);
        if (sysFd == -1) {
L
lnlan 已提交
542 543
            /* there are no more mq sysFd to use, close the personal */
            (VOID)DoMqueueClose(privateMqPersonal);
M
mamingshuai 已提交
544 545 546 547
            errno = ENFILE;
        }
        mqFd = (mqd_t)sysFd;
    }
L
lnlan 已提交
548
OUT:
Z
zhushengle 已提交
549
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
M
mamingshuai 已提交
550
    return mqFd;
W
wenjun 已提交
551 552 553 554
}

int mq_close(mqd_t personal)
{
M
mamingshuai 已提交
555
    INT32 ret = -1;
W
wenjun 已提交
556 557
    struct mqpersonal *privateMqPersonal = NULL;

Z
zhushengle 已提交
558
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
M
mamingshuai 已提交
559 560 561 562 563

    /* Get the personal sysFd and reset personal fd -1 */
    privateMqPersonal = MqGetPrivDataBuff(personal);
    if (privateMqPersonal == NULL) {
        goto OUT_UNLOCK;
W
wenjun 已提交
564 565 566 567 568 569
    }

    if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
        errno = EBADF;
        goto OUT_UNLOCK;
    }
L
lnlan 已提交
570

M
mamingshuai 已提交
571 572 573 574
    if (!MqTryClose(privateMqPersonal)) {
        ret = 0;
        goto OUT_UNLOCK;
    }
W
wenjun 已提交
575

L
lnlan 已提交
576 577
    ret = DoMqueueClose(privateMqPersonal);
    if (ret < 0) {
W
wenjun 已提交
578 579
        goto OUT_UNLOCK;
    }
L
lnlan 已提交
580
    MqFreeSysFd(personal);
W
wenjun 已提交
581 582

OUT_UNLOCK:
Z
zhushengle 已提交
583
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
584 585 586 587 588 589 590 591
    return ret;
}

int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr)
{
    struct mqarray *mqueueCB = NULL;
    struct mqpersonal *privateMqPersonal = NULL;

M
mamingshuai 已提交
592 593
    privateMqPersonal = MqGetPrivDataBuff(personal);
    if (privateMqPersonal == NULL) {
W
wenjun 已提交
594 595 596 597 598 599 600 601
        return -1;
    }

    if (mqAttr == NULL) {
        errno = EINVAL;
        return -1;
    }

Z
zhushengle 已提交
602
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
603 604
    if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
        errno = EBADF;
Z
zhushengle 已提交
605
        (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
606 607 608 609 610 611 612 613
        return -1;
    }

    mqueueCB = privateMqPersonal->mq_posixdes;
    mqAttr->mq_maxmsg = mqueueCB->mqcb->queueLen;
    mqAttr->mq_msgsize = mqueueCB->mqcb->queueSize - sizeof(UINT32);
    mqAttr->mq_curmsgs = mqueueCB->mqcb->readWriteableCnt[OS_QUEUE_READ];
    mqAttr->mq_flags = privateMqPersonal->mq_flags;
Z
zhushengle 已提交
614
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
615 616 617 618 619 620 621
    return 0;
}

int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr *mqOldAttr)
{
    struct mqpersonal *privateMqPersonal = NULL;

M
mamingshuai 已提交
622 623
    privateMqPersonal = MqGetPrivDataBuff(personal);
    if (privateMqPersonal == NULL) {
W
wenjun 已提交
624 625 626 627 628 629 630 631
        return -1;
    }

    if (mqSetAttr == NULL) {
        errno = EINVAL;
        return -1;
    }

Z
zhushengle 已提交
632
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
633 634
    if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
        errno = EBADF;
Z
zhushengle 已提交
635
        (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
636 637 638 639
        return -1;
    }

    if (mqOldAttr != NULL) {
M
mamingshuai 已提交
640
        (VOID)OsMqGetAttr(personal, mqOldAttr);
W
wenjun 已提交
641 642 643 644 645 646
    }

    privateMqPersonal->mq_flags = (INT32)((UINT32)privateMqPersonal->mq_flags & (UINT32)(~FNONBLOCK)); /* clear */
    if (((UINT32)mqSetAttr->mq_flags & (UINT32)FNONBLOCK) == (UINT32)FNONBLOCK) {
        privateMqPersonal->mq_flags = (INT32)((UINT32)privateMqPersonal->mq_flags | (UINT32)FNONBLOCK);
    }
Z
zhushengle 已提交
647
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
    return 0;
}

int mq_getsetattr(mqd_t mqd, const struct mq_attr *new, struct mq_attr *old)
{
    if (new == NULL) {
        return OsMqGetAttr(mqd, old);
    }
    return OsMqSetAttr(mqd, new, old);
}

int mq_unlink(const char *mqName)
{
    INT32 ret = 0;
    struct mqarray *mqueueCB = NULL;

    if (MqNameCheck(mqName) == -1) {
        return -1;
    }

Z
zhushengle 已提交
668
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
669 670 671 672 673
    mqueueCB = GetMqueueCBByName(mqName);
    if (mqueueCB == NULL) {
        errno = ENOENT;
        goto ERROUT_UNLOCK;
    }
L
lnlan 已提交
674

W
wenjun 已提交
675 676
    if (mqueueCB->mq_personal != NULL) {
        mqueueCB->unlinkflag = TRUE;
M
mamingshuai 已提交
677
    } else if (mqueueCB->unlink_ref == 0) {
W
wenjun 已提交
678 679 680
        ret = DoMqueueDelete(mqueueCB);
    }

Z
zhushengle 已提交
681
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
682 683 684
    return ret;

ERROUT_UNLOCK:
Z
zhushengle 已提交
685
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    return -1;
}

STATIC INT32 ConvertTimeout(long flags, const struct timespec *absTimeout, UINT64 *ticks)
{
    if ((UINT32)flags & (UINT32)FNONBLOCK) {
        *ticks = LOS_NO_WAIT;
        return 0;
    }

    if (absTimeout == NULL) {
        *ticks = LOS_WAIT_FOREVER;
        return 0;
    }

    if (!ValidTimeSpec(absTimeout)) {
        errno = EINVAL;
        return -1;
    }

    *ticks = OsTimeSpec2Tick(absTimeout);
    return 0;
}

STATIC INLINE BOOL MqParamCheck(mqd_t personal, const char *msg, size_t msgLen)
{
M
mamingshuai 已提交
712
    if (personal < 0) {
W
wenjun 已提交
713 714 715 716 717 718 719 720 721 722
        return FALSE;
    }

    if ((msg == NULL) || (msgLen == 0)) {
        errno = EINVAL;
        return FALSE;
    }
    return TRUE;
}

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
/*
 * Send realtime a signal to process which registered itself
 * successfully by mq_notify.
 */
static void MqSendNotify(struct mqarray *mqueueCB)
{
    struct mqnotify *mqnotify = &mqueueCB->mq_notify;

    if ((mqnotify->pid) && (mqueueCB->mqcb->readWriteableCnt[OS_QUEUE_READ] == 0)) {
        siginfo_t info;

        switch (mqnotify->notify.sigev_notify) {
            case SIGEV_SIGNAL:
                /* sends signal */
                /* Create the siginfo structure */
                info.si_signo = mqnotify->notify.sigev_signo;
                info.si_code = SI_MESGQ;
                info.si_value = mqnotify->notify.sigev_value;
                OsDispatch(mqnotify->pid, &info, OS_USER_KILL_PERMISSION);
                break;
            case SIGEV_NONE:
            default:
                break;
        }
        /* after notification unregisters process */
        mqnotify->pid = 0;
    }
}

W
wenjun 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
#define OS_MQ_GOTO_ERROUT_UNLOCK_IF(expr, errcode) \
    if (expr) {                        \
        errno = errcode;                 \
        goto ERROUT_UNLOCK;                     \
    }
#define OS_MQ_GOTO_ERROUT_IF(expr, errcode) \
    if (expr) {                        \
        errno = errcode;                 \
        goto ERROUT;                     \
    }
int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int msgPrio,
                 const struct timespec *absTimeout)
{
    UINT32 mqueueID, err;
    UINT64 absTicks;
    struct mqarray *mqueueCB = NULL;
    struct mqpersonal *privateMqPersonal = NULL;

    OS_MQ_GOTO_ERROUT_IF(!MqParamCheck(personal, msg, msgLen), errno);
    OS_MQ_GOTO_ERROUT_IF(msgPrio > (MQ_PRIO_MAX - 1), EINVAL);

Z
zhushengle 已提交
773
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
M
mamingshuai 已提交
774 775
    privateMqPersonal = MqGetPrivDataBuff(personal);

776
    OS_MQ_GOTO_ERROUT_UNLOCK_IF(privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC, EBADF);
W
wenjun 已提交
777 778 779 780 781 782 783 784 785 786

    mqueueCB = privateMqPersonal->mq_posixdes;
    OS_MQ_GOTO_ERROUT_UNLOCK_IF(msgLen > (size_t)(mqueueCB->mqcb->queueSize - sizeof(UINT32)), EMSGSIZE);

    OS_MQ_GOTO_ERROUT_UNLOCK_IF((((UINT32)privateMqPersonal->mq_flags & (UINT32)O_WRONLY) != (UINT32)O_WRONLY) &&
                                (((UINT32)privateMqPersonal->mq_flags & (UINT32)O_RDWR) != (UINT32)O_RDWR),
                                EBADF);

    OS_MQ_GOTO_ERROUT_UNLOCK_IF(ConvertTimeout(privateMqPersonal->mq_flags, absTimeout, &absTicks) == -1, errno);
    mqueueID = mqueueCB->mq_id;
Z
zhushengle 已提交
787
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
788

789 790 791 792
    if (LOS_ListEmpty(&mqueueCB->mqcb->readWriteList[OS_QUEUE_READ])) {
        MqSendNotify(mqueueCB);
    }

W
wenjun 已提交
793 794 795 796 797 798
    err = LOS_QueueWriteCopy(mqueueID, (VOID *)msg, (UINT32)msgLen, (UINT32)absTicks);
    if (map_errno(err) != ENOERR) {
        goto ERROUT;
    }
    return 0;
ERROUT_UNLOCK:
Z
zhushengle 已提交
799
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
ERROUT:
    return -1;
}

ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *msgPrio,
                        const struct timespec *absTimeout)
{
    UINT32 mqueueID, err;
    UINT32 receiveLen;
    UINT64 absTicks;
    struct mqarray *mqueueCB = NULL;
    struct mqpersonal *privateMqPersonal = NULL;

    if (!MqParamCheck(personal, msg, msgLen)) {
        goto ERROUT;
    }

    if (msgPrio != NULL) {
        *msgPrio = 0;
    }

Z
zhushengle 已提交
821
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
M
mamingshuai 已提交
822
    privateMqPersonal = MqGetPrivDataBuff(personal);
823
    if (privateMqPersonal == NULL || privateMqPersonal->mq_status != MQ_USE_MAGIC) {
W
wenjun 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
        errno = EBADF;
        goto ERROUT_UNLOCK;
    }

    mqueueCB = privateMqPersonal->mq_posixdes;
    if (msgLen < (size_t)(mqueueCB->mqcb->queueSize - sizeof(UINT32))) {
        errno = EMSGSIZE;
        goto ERROUT_UNLOCK;
    }

    if (((UINT32)privateMqPersonal->mq_flags & (UINT32)O_WRONLY) == (UINT32)O_WRONLY) {
        errno = EBADF;
        goto ERROUT_UNLOCK;
    }

    if (ConvertTimeout(privateMqPersonal->mq_flags, absTimeout, &absTicks) == -1) {
        goto ERROUT_UNLOCK;
    }

    receiveLen = msgLen;
    mqueueID = mqueueCB->mq_id;
Z
zhushengle 已提交
845
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
846 847 848 849 850 851 852 853 854

    err = LOS_QueueReadCopy(mqueueID, (VOID *)msg, &receiveLen, (UINT32)absTicks);
    if (map_errno(err) == ENOERR) {
        return (ssize_t)receiveLen;
    } else {
        goto ERROUT;
    }

ERROUT_UNLOCK:
Z
zhushengle 已提交
855
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
W
wenjun 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
ERROUT:
    return -1;
}

/* not support the prio */
int mq_send(mqd_t personal, const char *msg_ptr, size_t msg_len, unsigned int msg_prio)
{
    return mq_timedsend(personal, msg_ptr, msg_len, msg_prio, NULL);
}

ssize_t mq_receive(mqd_t personal, char *msg_ptr, size_t msg_len, unsigned int *msg_prio)
{
    return mq_timedreceive(personal, msg_ptr, msg_len, msg_prio, NULL);
}

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
STATIC INLINE BOOL MqNotifyParamCheck(mqd_t personal, const struct sigevent *sigev)
{
    if (personal < 0) {
        errno = EBADF;
        goto ERROUT;
    }

    if (sigev != NULL) {
        if (sigev->sigev_notify != SIGEV_NONE && sigev->sigev_notify != SIGEV_SIGNAL) {
            errno = EINVAL;
            goto ERROUT;
        }
        if (sigev->sigev_notify == SIGEV_SIGNAL && !GOOD_SIGNO(sigev->sigev_signo)) {
            errno = EINVAL;
            goto ERROUT;
        }
    }

    return TRUE;
ERROUT:
    return FALSE;
}

int OsMqNotify(mqd_t personal, const struct sigevent *sigev)
{
    struct mqarray *mqueueCB = NULL;
    struct mqnotify *mqnotify = NULL;
    struct mqpersonal *privateMqPersonal = NULL;

    if (!MqNotifyParamCheck(personal, sigev)) {
        goto ERROUT;
    }

Z
zhushengle 已提交
904
    (VOID)pthread_mutex_lock(&IPC_QUEUE_MUTEX);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    privateMqPersonal = MqGetPrivDataBuff(personal);
    if (privateMqPersonal == NULL) {
        goto OUT_UNLOCK;
    }

    if (privateMqPersonal->mq_status != MQ_USE_MAGIC) {
        errno = EBADF;
        goto OUT_UNLOCK;
    }

    mqueueCB = privateMqPersonal->mq_posixdes;
    mqnotify = &mqueueCB->mq_notify;

    if (sigev == NULL) {
        if (mqnotify->pid == LOS_GetCurrProcessID()) {
            mqnotify->pid = 0;
        }
    } else if (mqnotify->pid != 0) {
        errno = EBUSY;
        goto OUT_UNLOCK;
    } else {
        switch (sigev->sigev_notify) {
            case SIGEV_NONE:
                mqnotify->notify.sigev_notify = SIGEV_NONE;
                break;
            case SIGEV_SIGNAL:
                mqnotify->notify.sigev_signo = sigev->sigev_signo;
                mqnotify->notify.sigev_value = sigev->sigev_value;
                mqnotify->notify.sigev_notify = SIGEV_SIGNAL;
                break;
            default:
                break;
        }

        mqnotify->pid = LOS_GetCurrProcessID();
    }

Z
zhushengle 已提交
942
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
943 944
    return 0;
OUT_UNLOCK:
Z
zhushengle 已提交
945
    (VOID)pthread_mutex_unlock(&IPC_QUEUE_MUTEX);
946 947 948
ERROUT:
    return -1;
}
949
#endif