process_syscall.c 25.1 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 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 33
 *
 * 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 "los_process_pri.h"
#include "los_task_pri.h"
M
mamingshuai 已提交
34
#include "los_sched_pri.h"
W
wenjun 已提交
35 36 37
#include "los_hw_pri.h"
#include "los_sys_pri.h"
#include "los_futex_pri.h"
M
mamingshuai 已提交
38
#include "los_mp.h"
W
wjj 已提交
39
#include "sys/wait.h"
W
wenjun 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "user_copy.h"
#include "time.h"
#ifdef LOSCFG_SECURITY_CAPABILITY
#include "capability_api.h"
#endif

static int OsPermissionToCheck(unsigned int pid, unsigned int who)
{
    int ret = LOS_GetProcessGroupID(pid);
    if (ret < 0) {
        return ret;
    } else if (ret == OS_KERNEL_PROCESS_GROUP) {
        return -EPERM;
    } else if ((ret == OS_USER_PRIVILEGE_PROCESS_GROUP) && (pid != who)) {
        return -EPERM;
    } else if (pid == OsGetUserInitProcessID()) {
        return -EPERM;
    }

    return 0;
}

static int OsUserTaskSchedulerSet(unsigned int tid, unsigned short policy, unsigned short priority, bool policyFlag)
{
    int ret;
    unsigned int intSave;
M
mamingshuai 已提交
66
    bool needSched = false;
W
wenjun 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79

    if (OS_TID_CHECK_INVALID(tid)) {
        return EINVAL;
    }

    if (priority > OS_TASK_PRIORITY_LOWEST) {
        return EINVAL;
    }

    if ((policy != LOS_SCHED_FIFO) && (policy != LOS_SCHED_RR)) {
        return EINVAL;
    }

M
mamingshuai 已提交
80
    LosTaskCB *taskCB = OS_TCB_FROM_TID(tid);
Z
zhushengle 已提交
81
    SCHEDULER_LOCK(intSave);
W
wenjun 已提交
82 83 84 85 86 87
    ret = OsUserTaskOperatePermissionsCheck(taskCB);
    if (ret != LOS_OK) {
        SCHEDULER_UNLOCK(intSave);
        return ret;
    }

M
mamingshuai 已提交
88 89 90 91 92 93 94 95 96 97
    policy = (policyFlag == true) ? policy : taskCB->policy;
    needSched = OsSchedModifyTaskSchedParam(taskCB, policy, priority);
    SCHEDULER_UNLOCK(intSave);

    LOS_MpSchedule(OS_MP_CPU_ALL);
    if (needSched && OS_SCHEDULER_ACTIVE) {
        LOS_Schedule();
    }

    return LOS_OK;
W
wenjun 已提交
98 99 100 101
}

void SysSchedYield(int type)
{
Z
zhushengle 已提交
102 103
    (void)type;

M
mamingshuai 已提交
104
    (void)LOS_TaskYield();
W
wenjun 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118
    return;
}

int SysSchedGetScheduler(int id, int flag)
{
    unsigned int intSave;
    int policy;
    int ret;

    if (flag < 0) {
        if (OS_TID_CHECK_INVALID(id)) {
            return -EINVAL;
        }

Z
zhushengle 已提交
119
        LosTaskCB *taskCB = OS_TCB_FROM_TID(id);
W
wenjun 已提交
120 121 122 123 124 125 126
        SCHEDULER_LOCK(intSave);
        ret = OsUserTaskOperatePermissionsCheck(taskCB);
        if (ret != LOS_OK) {
            SCHEDULER_UNLOCK(intSave);
            return -ret;
        }

Z
zhushengle 已提交
127
        policy = (int)taskCB->policy;
W
wenjun 已提交
128 129 130 131 132 133 134 135 136 137 138 139
        SCHEDULER_UNLOCK(intSave);
        return policy;
    }

    return LOS_GetProcessScheduler(id);
}

int SysSchedSetScheduler(int id, int policy, int prio, int flag)
{
    int ret;

    if (flag < 0) {
M
mamingshuai 已提交
140
        return -OsUserTaskSchedulerSet(id, policy, prio, true);
W
wenjun 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }

    if (prio < OS_USER_PROCESS_PRIORITY_HIGHEST) {
        return -EINVAL;
    }

    if (id == 0) {
        id = (int)LOS_GetCurrProcessID();
    }

    ret = OsPermissionToCheck(id, LOS_GetCurrProcessID());
    if (ret < 0) {
        return ret;
    }

M
mamingshuai 已提交
156
    return OsSetProcessScheduler(LOS_PRIO_PROCESS, id, prio, policy);
W
wenjun 已提交
157 158 159 160
}

int SysSchedGetParam(int id, int flag)
{
Z
zhushengle 已提交
161
    int prio;
W
wenjun 已提交
162 163 164 165 166 167 168
    unsigned int intSave;

    if (flag < 0) {
        if (OS_TID_CHECK_INVALID(id)) {
            return -EINVAL;
        }

Z
zhushengle 已提交
169
        LosTaskCB *taskCB = OS_TCB_FROM_TID(id);
W
wenjun 已提交
170
        SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
171 172
        prio = OsUserTaskOperatePermissionsCheck(taskCB);
        if (prio != LOS_OK) {
W
wenjun 已提交
173
            SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
174
            return -prio;
W
wenjun 已提交
175 176
        }

Z
zhushengle 已提交
177
        prio = (int)taskCB->priority;
W
wenjun 已提交
178
        SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
179
        return prio;
W
wenjun 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192
    }

    if (id == 0) {
        id = (int)LOS_GetCurrProcessID();
    }

    if (OS_PID_CHECK_INVALID(id)) {
        return -EINVAL;
    }

    return OsGetProcessPriority(LOS_PRIO_PROCESS, id);
}

M
mamingshuai 已提交
193
int SysSetProcessPriority(int which, int who, unsigned int prio)
W
wenjun 已提交
194 195 196 197 198 199 200
{
    int ret;

    if (prio < OS_USER_PROCESS_PRIORITY_HIGHEST) {
        return -EINVAL;
    }

M
mamingshuai 已提交
201 202
    if (who == 0) {
        who = (int)LOS_GetCurrProcessID();
W
wenjun 已提交
203 204
    }

M
mamingshuai 已提交
205
    ret = OsPermissionToCheck(who, LOS_GetCurrProcessID());
W
wenjun 已提交
206 207 208 209
    if (ret < 0) {
        return ret;
    }

M
mamingshuai 已提交
210
    return OsSetProcessScheduler(which, who, prio, LOS_GetProcessScheduler(who));
W
wenjun 已提交
211 212
}

M
mamingshuai 已提交
213
int SysSchedSetParam(int id, unsigned int prio, int flag)
W
wenjun 已提交
214
{
M
mamingshuai 已提交
215 216
    if (flag < 0) {
        return -OsUserTaskSchedulerSet(id, LOS_SCHED_RR, prio, false);
W
wenjun 已提交
217 218
    }

M
mamingshuai 已提交
219
    return SysSetProcessPriority(LOS_PRIO_PROCESS, id, prio);
W
wenjun 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
}

int SysGetProcessPriority(int which, int who)
{
    if (who == 0) {
        who = (int)LOS_GetCurrProcessID();
    }

    return OsGetProcessPriority(which, who);
}

int SysSchedGetPriorityMin(int policy)
{
M
mamingshuai 已提交
233
    if (policy != LOS_SCHED_RR) {
W
wenjun 已提交
234 235 236 237 238 239 240 241
        return -EINVAL;
    }

    return OS_USER_PROCESS_PRIORITY_HIGHEST;
}

int SysSchedGetPriorityMax(int policy)
{
M
mamingshuai 已提交
242
    if (policy != LOS_SCHED_RR) {
W
wenjun 已提交
243 244 245 246 247 248 249 250
        return -EINVAL;
    }

    return OS_USER_PROCESS_PRIORITY_LOWEST;
}

int SysSchedRRGetInterval(int pid, struct timespec *tp)
{
M
mamingshuai 已提交
251
    unsigned int intSave;
W
wenjun 已提交
252
    int ret;
M
mamingshuai 已提交
253
    time_t timeSlice = 0;
W
wenjun 已提交
254
    struct timespec tv;
M
mamingshuai 已提交
255 256
    LosTaskCB *taskCB = NULL;
    LosProcessCB *processCB = NULL;
W
wenjun 已提交
257 258 259 260 261 262 263 264 265

    if (tp == NULL) {
        return -EINVAL;
    }

    if (OS_PID_CHECK_INVALID(pid)) {
        return -EINVAL;
    }

M
mamingshuai 已提交
266 267 268 269 270 271 272
    if (pid == 0) {
        processCB = OsCurrProcessGet();
    } else {
        processCB = OS_PCB_FROM_PID(pid);
    }

    SCHEDULER_LOCK(intSave);
W
wenjun 已提交
273
    /* if can not find process by pid return ESRCH */
M
mamingshuai 已提交
274 275
    if (OsProcessIsInactive(processCB)) {
        SCHEDULER_UNLOCK(intSave);
W
wenjun 已提交
276 277 278
        return -ESRCH;
    }

M
mamingshuai 已提交
279 280 281 282 283 284 285 286 287 288 289
    LOS_DL_LIST_FOR_EACH_ENTRY(taskCB, &processCB->threadSiblingList, LosTaskCB, threadList) {
        if (!OsTaskIsInactive(taskCB) && (taskCB->policy == LOS_SCHED_RR)) {
            timeSlice += taskCB->initTimeSlice;
        }
    }

    SCHEDULER_UNLOCK(intSave);

    timeSlice = timeSlice * OS_NS_PER_CYCLE;
    tv.tv_sec = timeSlice / OS_SYS_NS_PER_SECOND;
    tv.tv_nsec = timeSlice % OS_SYS_NS_PER_SECOND;
W
wenjun 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    ret = LOS_ArchCopyToUser(tp, &tv, sizeof(struct timespec));
    if (ret != 0) {
        return -EFAULT;
    }

    return 0;
}

int SysWait(int pid, USER int *status, int options, void *rusage)
{
    (void)rusage;

    return LOS_Wait(pid, status, (unsigned int)options, NULL);
}

W
wjj 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
int SysWaitid(idtype_t type, int pid, USER siginfo_t *info, int options, void *rusage)
{
    (void)rusage;
    int ret;
    int truepid = 0;

    switch (type) {
        case P_ALL:
            /* Wait for any child; id is ignored. */
            truepid = -1;
            break;
        case P_PID:
            /* Wait for the child whose process ID matches id */
            if (pid <= 0) {
                return -EINVAL;
            }
            truepid = pid;
            break;
        case P_PGID:
            /* Wait for any child whose process group ID matches id */
            if (pid <= 1) {
                return -EINVAL;
            }
            truepid = -pid;
            break;
        default:
            return -EINVAL;
    }

    ret = LOS_Waitid(truepid, info, (unsigned int)options, NULL);
    if (ret > 0) {
        ret = 0;
    }
    return ret;
}

W
wenjun 已提交
341 342
int SysFork(void)
{
星e雨's avatar
星e雨 已提交
343
    return OsClone(0, 0, 0);
W
wenjun 已提交
344 345
}

G
Guangyao Ma 已提交
346 347 348 349 350
int SysVfork(void)
{
    return OsClone(CLONE_VFORK, 0, 0);
}

W
wenjun 已提交
351 352 353 354 355 356 357 358 359 360 361 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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 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 712 713 714 715 716 717 718 719 720 721 722 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 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
unsigned int SysGetPPID(void)
{
    return OsCurrProcessGet()->parentProcessID;
}

unsigned int SysGetPID(void)
{
    return LOS_GetCurrProcessID();
}

int SysSetProcessGroupID(unsigned int pid, unsigned int gid)
{
    int ret;

    if (pid == 0) {
        pid = LOS_GetCurrProcessID();
    }

    if (gid == 0) {
        gid = pid;
    } else if (gid <= OS_USER_PRIVILEGE_PROCESS_GROUP) {
        return -EPERM;
    }

    ret = OsPermissionToCheck(pid, gid);
    if (ret < 0) {
        return ret;
    }

    return OsSetProcessGroupID(pid, gid);
}

int SysGetProcessGroupID(unsigned int pid)
{
    if (pid == 0) {
        pid = LOS_GetCurrProcessID();
    }

    return LOS_GetProcessGroupID(pid);
}

int SysGetCurrProcessGroupID(void)
{
    return LOS_GetCurrProcessGroupID();
}

int SysGetUserID(void)
{
    return LOS_GetUserID();
}

int SysGetEffUserID(void)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    UINT32 intSave;
    int euid;

    SCHEDULER_LOCK(intSave);
    euid = (int)OsCurrUserGet()->effUserID;
    SCHEDULER_UNLOCK(intSave);
    return euid;
#else
    return 0;
#endif
}

int SysGetEffGID(void)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    UINT32 intSave;
    int egid;

    SCHEDULER_LOCK(intSave);
    egid = (int)OsCurrUserGet()->effGid;
    SCHEDULER_UNLOCK(intSave);
    return egid;
#else
    return 0;
#endif
}

int SysGetRealEffSaveUserID(int *ruid, int *euid, int *suid)
{
    int ret;
    int realUserID, effUserID, saveUserID;
#ifdef LOSCFG_SECURITY_CAPABILITY
    unsigned int intSave;

    SCHEDULER_LOCK(intSave);
    realUserID = OsCurrUserGet()->userID;
    effUserID = OsCurrUserGet()->effUserID;
    saveUserID = OsCurrUserGet()->effUserID;
    SCHEDULER_UNLOCK(intSave);
#else
    realUserID = 0;
    effUserID = 0;
    saveUserID = 0;
#endif

    ret = LOS_ArchCopyToUser(ruid, &realUserID, sizeof(int));
    if (ret != 0) {
        return -EFAULT;
    }

    ret = LOS_ArchCopyToUser(euid, &effUserID, sizeof(int));
    if (ret != 0) {
        return -EFAULT;
    }

    ret = LOS_ArchCopyToUser(suid, &saveUserID, sizeof(int));
    if (ret != 0) {
        return -EFAULT;
    }

    return 0;
}

int SysSetUserID(int uid)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    int ret = -EPERM;
    unsigned int intSave;

    if (uid < 0) {
        return -EINVAL;
    }

    SCHEDULER_LOCK(intSave);
    User *user = OsCurrUserGet();
    if (IsCapPermit(CAP_SETUID)) {
        user->userID = uid;
        user->effUserID = uid;
        /* add process to a user */
    } else if (user->userID != uid) {
        goto EXIT;
    }

    ret = LOS_OK;
    /* add process to a user */
EXIT:
    SCHEDULER_UNLOCK(intSave);
    return ret;
#else
    if (uid != 0) {
        return -EPERM;
    }

    return 0;
#endif
}

#ifdef LOSCFG_SECURITY_CAPABILITY
static int SetRealEffSaveUserIDCheck(int ruid, int euid, int suid)
{
    if ((ruid < 0) && (ruid != -1)) {
        return -EINVAL;
    }

    if ((euid < 0) && (euid != -1)) {
        return -EINVAL;
    }

    if ((suid < 0) && (suid != -1)) {
        return -EINVAL;
    }

    return 0;
}
#endif

int SysSetRealEffSaveUserID(int ruid, int euid, int suid)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    int ret;

    if ((ruid == -1) && (euid == -1) && (suid == -1)) {
        return 0;
    }

    ret = SetRealEffSaveUserIDCheck(ruid, euid, suid);
    if (ret != 0) {
        return ret;
    }

    if (ruid >= 0) {
        if (((euid != -1) && (euid != ruid)) || ((suid != -1) && (suid != ruid))) {
            return -EPERM;
        }
        return SysSetUserID(ruid);
    } else if (euid >= 0) {
        if ((suid != -1) && (suid != euid)) {
            return -EPERM;
        }
        return SysSetUserID(euid);
    } else {
        return SysSetUserID(suid);
    }
#else
    if ((ruid != 0) || (euid != 0) || (suid != 0)) {
        return -EPERM;
    }
    return 0;
#endif
}

int SysSetRealEffUserID(int ruid, int euid)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    return SysSetRealEffSaveUserID(ruid, euid, -1);
#else
    if ((ruid != 0) || (euid != 0)) {
        return -EPERM;
    }
    return 0;
#endif
}

int SysSetGroupID(int gid)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    int ret = -EPERM;
    unsigned int intSave;
    unsigned int count;
    unsigned int oldGid;
    User *user = NULL;

    if (gid < 0) {
        return -EINVAL;
    }

    SCHEDULER_LOCK(intSave);
    user = OsCurrUserGet();
    if (IsCapPermit(CAP_SETGID)) {
        oldGid = user->gid;
        user->gid = gid;
        user->effGid = gid;
        for (count = 0; count < user->groupNumber; count++) {
            if (user->groups[count] == oldGid) {
                user->groups[count] = gid;
                ret = LOS_OK;
                goto EXIT;
            }
        }
    } else if (user->gid != gid) {
        goto EXIT;
    }

    ret = LOS_OK;
    /* add process to a user */
EXIT:
    SCHEDULER_UNLOCK(intSave);
    return ret;

#else
    if (gid != 0) {
        return -EPERM;
    }

    return 0;
#endif
}

int SysGetRealEffSaveGroupID(int *rgid, int *egid, int *sgid)
{
    int ret;
    int realGroupID, effGroupID, saveGroupID;
#ifdef LOSCFG_SECURITY_CAPABILITY
    unsigned int intSave;

    SCHEDULER_LOCK(intSave);
    realGroupID = OsCurrUserGet()->gid;
    effGroupID = OsCurrUserGet()->effGid;
    saveGroupID = OsCurrUserGet()->effGid;
    SCHEDULER_UNLOCK(intSave);
#else
    realGroupID = 0;
    effGroupID = 0;
    saveGroupID = 0;
#endif

    ret = LOS_ArchCopyToUser(rgid, &realGroupID, sizeof(int));
    if (ret != 0) {
        return -EFAULT;
    }

    ret = LOS_ArchCopyToUser(egid, &effGroupID, sizeof(int));
    if (ret != 0) {
        return -EFAULT;
    }

    ret = LOS_ArchCopyToUser(sgid, &saveGroupID, sizeof(int));
    if (ret != 0) {
        return -EFAULT;
    }

    return 0;
}

#ifdef LOSCFG_SECURITY_CAPABILITY
static int SetRealEffSaveGroupIDCheck(int rgid, int egid, int sgid)
{
    if ((rgid < 0) && (rgid != -1)) {
        return -EINVAL;
    }

    if ((egid < 0) && (egid != -1)) {
        return -EINVAL;
    }

    if ((sgid < 0) && (sgid != -1)) {
        return -EINVAL;
    }

    return 0;
}
#endif

int SysSetRealEffSaveGroupID(int rgid, int egid, int sgid)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    int ret;

    if ((rgid == -1) && (egid == -1) && (sgid == -1)) {
        return 0;
    }

    ret = SetRealEffSaveGroupIDCheck(rgid, egid, sgid);
    if (ret != 0) {
        return ret;
    }

    if (rgid >= 0) {
        if (((egid != -1) && (egid != rgid)) || ((sgid != -1) && (sgid != rgid))) {
            return -EPERM;
        }
        return SysSetGroupID(rgid);
    } else if (egid >= 0) {
        if ((sgid != -1) && (sgid != egid)) {
            return -EPERM;
        }
        return SysSetGroupID(egid);
    } else {
        return SysSetGroupID(sgid);
    }

#else
    if ((rgid != 0) || (egid != 0) || (sgid != 0)) {
        return -EPERM;
    }
    return 0;
#endif
}

int SysSetRealEffGroupID(int rgid, int egid)
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    return SysSetRealEffSaveGroupID(rgid, egid, -1);
#else
    if ((rgid != 0) || (egid != 0)) {
        return -EPERM;
    }
    return 0;
#endif
}

int SysGetGroupID(void)
{
    return LOS_GetGroupID();
}

#ifdef LOSCFG_SECURITY_CAPABILITY
static int SetGroups(int listSize, const int *safeList, int size)
{
    User *oldUser = NULL;
    unsigned int intSave;

    User *newUser = LOS_MemAlloc(m_aucSysMem1, sizeof(User) + listSize * sizeof(int));
    if (newUser == NULL) {
        return -ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    oldUser = OsCurrUserGet();
    (VOID)memcpy_s(newUser, sizeof(User), oldUser, sizeof(User));
    if (safeList != NULL) {
        (VOID)memcpy_s(newUser->groups, size * sizeof(int), safeList, size * sizeof(int));
    }
    if (listSize == size) {
        newUser->groups[listSize] = oldUser->gid;
    }

    newUser->groupNumber = listSize + 1;
    OsCurrProcessGet()->user = newUser;
    SCHEDULER_UNLOCK(intSave);

    (void)LOS_MemFree(m_aucSysMem1, oldUser);
    return 0;
}

static int GetGroups(int size, int list[])
{
    unsigned int intSave;
    int groupCount;
    int ret;
    int *safeList = NULL;
    unsigned int listSize;

    SCHEDULER_LOCK(intSave);
    groupCount = OsCurrUserGet()->groupNumber;
    SCHEDULER_UNLOCK(intSave);

    listSize = groupCount * sizeof(int);
    if (size == 0) {
        return groupCount;
    } else if (list == NULL) {
        return -EFAULT;
    } else if (size < groupCount) {
        return -EINVAL;
    }

    safeList = LOS_MemAlloc(m_aucSysMem1, listSize);
    if (safeList == NULL) {
        return -ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    (void)memcpy_s(safeList, listSize, &OsCurrProcessGet()->user->groups[0], listSize);
    SCHEDULER_UNLOCK(intSave);

    ret = LOS_ArchCopyToUser(list, safeList, listSize);
    if (ret != 0) {
        groupCount = -EFAULT;
    }

    (void)LOS_MemFree(m_aucSysMem1, safeList);
    return groupCount;
}
#endif

int SysGetGroups(int size, int list[])
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    return GetGroups(size, list);
#else
    int group = 0;
    int groupCount = 1;
    int ret;

    if (size == 0) {
        return groupCount;
    } else if (list == NULL) {
        return -EFAULT;
    } else if (size < groupCount) {
        return -EINVAL;
    }

    ret = LOS_ArchCopyToUser(list, &group, sizeof(int));
    if (ret != 0) {
        return -EFAULT;
    }

    return groupCount;
#endif
}

M
mamingshuai 已提交
816
int SysSetGroups(int size, const int list[])
W
wenjun 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
{
#ifdef LOSCFG_SECURITY_CAPABILITY
    int ret;
    int gid;
    int listSize = size;
    unsigned int count;
    int *safeList = NULL;
#endif

    if ((size != 0) && (list == NULL)) {
        return -EFAULT;
    }

    if ((size < 0) || (size > OS_GROUPS_NUMBER_MAX)) {
        return -EINVAL;
    }

#ifdef LOSCFG_SECURITY_CAPABILITY
    if (!IsCapPermit(CAP_SETGID)) {
        return -EPERM;
    }

    if (size != 0) {
        safeList = LOS_MemAlloc(m_aucSysMem1, size * sizeof(int));
        if (safeList == NULL) {
            return -ENOMEM;
        }

        ret = LOS_ArchCopyFromUser(safeList, list, size * sizeof(int));
        if (ret != 0) {
            ret = -EFAULT;
            goto EXIT;
        }
        gid = OsCurrUserGet()->gid;
        for (count = 0; count < size; count++) {
            if (safeList[count] == gid) {
                listSize = size - 1;
            } else if (safeList[count] < 0) {
                ret = -EINVAL;
                goto EXIT;
            }
        }
    }

    ret = SetGroups(listSize, safeList, size);
EXIT:
    if (safeList != NULL) {
        (void)LOS_MemFree(m_aucSysMem1, safeList);
    }

    return ret;
#else
    return 0;
#endif
}

unsigned int SysCreateUserThread(const TSK_ENTRY_FUNC func, const UserTaskParam *userParam, bool joinable)
{
    TSK_INIT_PARAM_S param = { 0 };
    int ret;

    ret = LOS_ArchCopyFromUser(&(param.userParam), userParam, sizeof(UserTaskParam));
    if (ret != 0) {
        return OS_INVALID_VALUE;
    }

    param.pfnTaskEntry = func;
    if (joinable == TRUE) {
885
        param.uwResved = LOS_TASK_ATTR_JOINABLE;
W
wenjun 已提交
886
    } else {
887
        param.uwResved = LOS_TASK_STATUS_DETACHED;
W
wenjun 已提交
888 889 890 891 892 893 894 895
    }

    return OsCreateUserTask(OS_INVALID_VALUE, &param);
}

int SysSetThreadArea(const char *area)
{
    unsigned int intSave;
Z
zhushengle 已提交
896
    int ret = LOS_OK;
W
wenjun 已提交
897 898 899 900 901

    if (!LOS_IsUserAddress((unsigned long)(uintptr_t)area)) {
        return EINVAL;
    }

Z
zhushengle 已提交
902
    LosTaskCB *taskCB = OsCurrTaskGet();
W
wenjun 已提交
903
    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
904
    LosProcessCB *processCB = OS_PCB_FROM_PID(taskCB->processID);
W
wenjun 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    if (processCB->processMode != OS_USER_MODE) {
        ret = EPERM;
        goto OUT;
    }

    taskCB->userArea = (unsigned long)(uintptr_t)area;
OUT:
    SCHEDULER_UNLOCK(intSave);
    return ret;
}

char *SysGetThreadArea(void)
{
    return (char *)(OsCurrTaskGet()->userArea);
}

921
int SysUserThreadSetDetach(unsigned int taskID)
W
wenjun 已提交
922 923 924 925 926 927 928 929
{
    unsigned int intSave;
    int ret;

    if (OS_TID_CHECK_INVALID(taskID)) {
        return EINVAL;
    }

Z
zhushengle 已提交
930
    LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
W
wenjun 已提交
931 932 933 934 935 936
    SCHEDULER_LOCK(intSave);
    ret = OsUserTaskOperatePermissionsCheck(taskCB);
    if (ret != LOS_OK) {
        goto EXIT;
    }

Z
zhushengle 已提交
937
    ret = (int)OsTaskSetDetachUnsafe(taskCB);
W
wenjun 已提交
938 939 940 941 942 943 944 945 946

EXIT:
    SCHEDULER_UNLOCK(intSave);
    return ret;
}

int SysUserThreadDetach(unsigned int taskID)
{
    unsigned int intSave;
Z
zhushengle 已提交
947
    int ret;
W
wenjun 已提交
948 949 950 951 952 953

    if (OS_TID_CHECK_INVALID(taskID)) {
        return EINVAL;
    }

    SCHEDULER_LOCK(intSave);
954 955
    ret = OsUserTaskOperatePermissionsCheck(OS_TCB_FROM_TID(taskID));
    SCHEDULER_UNLOCK(intSave);
W
wenjun 已提交
956 957 958 959
    if (ret != LOS_OK) {
        return ret;
    }

Z
zhushengle 已提交
960
    if (LOS_TaskDelete(taskID) != LOS_OK) {
W
wenjun 已提交
961 962 963 964 965 966 967 968 969
        return ESRCH;
    }

    return LOS_OK;
}

int SysThreadJoin(unsigned int taskID)
{
    unsigned int intSave;
Z
zhushengle 已提交
970
    int ret;
W
wenjun 已提交
971 972 973 974 975

    if (OS_TID_CHECK_INVALID(taskID)) {
        return EINVAL;
    }

Z
zhushengle 已提交
976
    LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
W
wenjun 已提交
977 978 979 980 981 982
    SCHEDULER_LOCK(intSave);
    ret = OsUserTaskOperatePermissionsCheck(taskCB);
    if (ret != LOS_OK) {
        goto EXIT;
    }

Z
zhushengle 已提交
983
    ret = (int)OsTaskJoinPendUnsafe(OS_TCB_FROM_TID(taskID));
W
wenjun 已提交
984 985 986 987 988 989 990 991

EXIT:
    SCHEDULER_UNLOCK(intSave);
    return ret;
}

void SysUserExitGroup(int status)
{
992 993
    (void)status;
    OsProcessThreadGroupDestroy();
W
wenjun 已提交
994 995 996 997
}

void SysThreadExit(int status)
{
998
    OsRunningTaskToExit(OsCurrTaskGet(), (unsigned int)status);
W
wenjun 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
}

int SysFutex(const unsigned int *uAddr, unsigned int flags, int val,
             unsigned int absTime, const unsigned int *newUserAddr)
{
    if ((flags & FUTEX_MASK) == FUTEX_REQUEUE) {
        return -OsFutexRequeue(uAddr, flags, val, absTime, newUserAddr);
    }

    if ((flags & FUTEX_MASK) == FUTEX_WAKE) {
        return -OsFutexWake(uAddr, flags, val);
    }

    return -OsFutexWait(uAddr, flags, val, absTime);
}

unsigned int SysGetTid(void)
{
    return OsCurrTaskGet()->taskID;
}

M
mamingshuai 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
/* If flag >= 0, the process mode is used. If flag < 0, the thread mode is used. */
static int SchedAffinityParameterPreprocess(int id, int flag, unsigned int *taskID, unsigned int *processID)
{
    if (flag >= 0) {
        if (OS_PID_CHECK_INVALID(id)) {
            return -ESRCH;
        }
        *taskID = (id == 0) ? (OsCurrTaskGet()->taskID) : (OS_PCB_FROM_PID((UINT32)id)->threadGroupID);
        *processID = (id == 0) ? (OS_TCB_FROM_TID(*taskID)->processID) : id;
    } else {
        if (OS_TID_CHECK_INVALID(id)) {
            return -ESRCH;
        }
        *taskID = id;
        *processID = OS_INVALID_VALUE;
    }
    return LOS_OK;
}

/* If flag >= 0, the process mode is used. If flag < 0, the thread mode is used. */
int SysSchedGetAffinity(int id, unsigned int *cpuset, int flag)
{
    int ret;
    unsigned int processID;
    unsigned int taskID;
    unsigned int intSave;
    unsigned int cpuAffiMask;

    ret = SchedAffinityParameterPreprocess(id, flag, &taskID, &processID);
    if (ret != LOS_OK) {
        return ret;
    }

    SCHEDULER_LOCK(intSave);
    if (flag >= 0) {
        if (OsProcessIsInactive(OS_PCB_FROM_PID(processID))) {
            SCHEDULER_UNLOCK(intSave);
            return -ESRCH;
        }
    } else {
        ret = OsUserTaskOperatePermissionsCheck(OS_TCB_FROM_TID(taskID));
        if (ret != LOS_OK) {
            SCHEDULER_UNLOCK(intSave);
            if (ret == EINVAL) {
                return -ESRCH;
            }
            return -ret;
        }
    }

1070
#ifdef LOSCFG_KERNEL_SMP
M
mamingshuai 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    cpuAffiMask = (unsigned int)OS_TCB_FROM_TID(taskID)->cpuAffiMask;
#else
    cpuAffiMask = 1;
#endif /* LOSCFG_KERNEL_SMP */

    SCHEDULER_UNLOCK(intSave);
    ret = LOS_ArchCopyToUser(cpuset, &cpuAffiMask, sizeof(unsigned int));
    if (ret != LOS_OK) {
        return -EFAULT;
    }

    return LOS_OK;
}

/* If flag >= 0, the process mode is used. If flag < 0, the thread mode is used. */
int SysSchedSetAffinity(int id, const unsigned short cpuset, int flag)
{
    int ret;
    unsigned int processID;
    unsigned int taskID;
    unsigned int intSave;
    unsigned short currCpuMask;
    bool needSched = FALSE;

    if (cpuset > LOSCFG_KERNEL_CPU_MASK) {
        return -EINVAL;
    }

    ret = SchedAffinityParameterPreprocess(id, flag, &taskID, &processID);
    if (ret != LOS_OK) {
        return ret;
    }

    if (flag >= 0) {
        ret = OsPermissionToCheck(processID, LOS_GetCurrProcessID());
        if (ret != LOS_OK) {
            return ret;
        }
        SCHEDULER_LOCK(intSave);
        if (OsProcessIsInactive(OS_PCB_FROM_PID(processID))) {
            SCHEDULER_UNLOCK(intSave);
            return -ESRCH;
        }
    } else {
        SCHEDULER_LOCK(intSave);
        ret = OsUserTaskOperatePermissionsCheck(OS_TCB_FROM_TID(taskID));
        if (ret != LOS_OK) {
            SCHEDULER_UNLOCK(intSave);
            if (ret == EINVAL) {
                return -ESRCH;
            }
            return -ret;
        }
    }

    needSched = OsTaskCpuAffiSetUnsafe(taskID, cpuset, &currCpuMask);
    SCHEDULER_UNLOCK(intSave);
    if (needSched && OS_SCHEDULER_ACTIVE) {
        LOS_MpSchedule(currCpuMask);
        LOS_Schedule();
    }

    return LOS_OK;
}