process_proc.c 20.8 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
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
 *
 * 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.
 */

M
mamingshuai 已提交
32 33 34
#include <sys/statfs.h>
#include <sys/mount.h>
#include "proc_fs.h"
35
#include "internal.h"
M
mamingshuai 已提交
36
#include "los_process_pri.h"
Z
zhushengle 已提交
37 38
#include "user_copy.h"
#include "los_memory.h"
W
wenjun 已提交
39

40
#ifdef LOSCFG_PROC_PROCESS_DIR
41 42
#include "los_vm_dump.h"

43 44
typedef enum {
    PROC_PID,
45 46 47 48
    PROC_PID_MEM,
#ifdef LOSCFG_KERNEL_CPUP
    PROC_PID_CPUP,
#endif
Z
zhushengle 已提交
49 50 51 52 53
#ifdef LOSCFG_USER_CONTAINER
    PROC_UID_MAP,
    PROC_GID_MAP,
#endif
    PROC_P_TYPE_MAX,
54 55 56 57 58 59 60 61 62 63 64 65 66 67
} ProcessDataType;

struct ProcProcess {
    char         *name;
    mode_t       mode;
    int          type;
    const struct ProcFileOperations *fileOps;
};

struct ProcessData {
    uintptr_t process;
    unsigned int type;
};

Z
zhushengle 已提交
68 69 70 71 72 73 74 75
static LosProcessCB *ProcGetProcessCB(struct ProcessData *data)
{
    if (data->process != 0) {
        return (LosProcessCB *)data->process;
    }
    return OsCurrProcessGet();
}

76 77 78 79 80 81
#define PROC_PID_PRIVILEGE 7
#define PROC_PID_DIR_LEN 100
#ifdef LOSCFG_KERNEL_CONTAINER
static ssize_t ProcessContainerLink(unsigned int containerID, ContainerType type, char *buffer, size_t bufLen)
{
    ssize_t count = -1;
Z
zhushengle 已提交
82
    if ((type == PID_CONTAINER) || (type == PID_CHILD_CONTAINER)) {
83 84 85
        count = snprintf_s(buffer, bufLen, bufLen - 1, "'pid:[%u]'", containerID);
    } else if (type == UTS_CONTAINER) {
        count = snprintf_s(buffer, bufLen, bufLen - 1, "'uts:[%u]'", containerID);
86 87
    } else if (type == MNT_CONTAINER) {
        count = snprintf_s(buffer, bufLen, bufLen - 1, "'mnt:[%u]'", containerID);
Z
zhushengle 已提交
88 89
    } else if (type == IPC_CONTAINER) {
        count = snprintf_s(buffer, bufLen, bufLen - 1, "'ipc:[%u]'", containerID);
Z
zhushengle 已提交
90 91
    } else if ((type == TIME_CONTAINER) || (type == TIME_CHILD_CONTAINER)) {
        count = snprintf_s(buffer, bufLen, bufLen - 1, "'time:[%u]'", containerID);
Z
zhushengle 已提交
92 93
    } else if (type == USER_CONTAINER) {
        count = snprintf_s(buffer, bufLen, bufLen - 1, "'user:[%u]'", containerID);
Z
zhushengle 已提交
94 95
    }  else if (type == NET_CONTAINER) {
        count = snprintf_s(buffer, bufLen, bufLen - 1, "'net:[%u]'", containerID);
96 97 98 99 100 101 102 103 104 105
    }

    if (count < 0) {
        return -EBADF;
    }
    return count;
}

static ssize_t ProcessContainerReadLink(struct ProcDirEntry *entry, char *buffer, size_t bufLen)
{
Z
zhushengle 已提交
106 107
    char *freeBuf = NULL;
    char *buf = buffer;
108 109 110 111 112 113 114 115 116
    ssize_t count;
    unsigned int intSave;
    if (entry == NULL) {
        return -EINVAL;
    }
    struct ProcessData *data = (struct ProcessData *)entry->data;
    if (data == NULL) {
        return -EINVAL;
    }
Z
zhushengle 已提交
117 118 119 120 121 122 123 124 125 126

    if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)buffer, bufLen)) {
        buf = LOS_MemAlloc(m_aucSysMem1, bufLen);
        if (buf == NULL) {
            return -ENOMEM;
        }
        (void)memset_s(buf, bufLen, 0, bufLen);
        freeBuf = buf;
    }

Z
zhushengle 已提交
127
    LosProcessCB *processCB = ProcGetProcessCB(data);
128
    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
129
    UINT32 containerID = OsGetContainerID(processCB, (ContainerType)data->type);
130 131
    SCHEDULER_UNLOCK(intSave);
    if (containerID != OS_INVALID_VALUE) {
Z
zhushengle 已提交
132 133 134 135 136 137 138
        count = ProcessContainerLink(containerID, (ContainerType)data->type, buf, bufLen);
    } else {
        count = strlen("(unknown)");
        if (memcpy_s(buf, bufLen, "(unknown)", count + 1) != EOK) {
            (void)LOS_MemFree(m_aucSysMem1, freeBuf);
            return -EBADF;
        }
139
    }
Z
zhushengle 已提交
140 141 142 143 144 145 146 147 148 149
    if (count < 0) {
        (void)LOS_MemFree(m_aucSysMem1, freeBuf);
        return count;
    }

    if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)buffer, bufLen)) {
        if (LOS_ArchCopyToUser(buffer, buf, bufLen) != 0) {
            (void)LOS_MemFree(m_aucSysMem1, freeBuf);
            return -EFAULT;
        }
150
    }
Z
zhushengle 已提交
151
    (void)LOS_MemFree(m_aucSysMem1, freeBuf);
152 153 154 155 156 157
    return count;
}

static const struct ProcFileOperations PID_CONTAINER_FOPS = {
    .readLink = ProcessContainerReadLink,
};
Z
zhushengle 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

void *ProcfsContainerGet(int fd, unsigned int *containerType)
{
    if ((fd <= 0) || (containerType == NULL)) {
        return NULL;
    }

    VnodeHold();
    struct Vnode *vnode = VnodeFind(fd);
    if (vnode == NULL) {
        VnodeDrop();
        return NULL;
    }

    struct ProcDirEntry *entry = VnodeToEntry(vnode);
    if (entry == NULL) {
        VnodeDrop();
        return NULL;
    }

    struct ProcessData *data = (struct ProcessData *)entry->data;
    if (data == NULL) {
        VnodeDrop();
        return NULL;
    }

    void *processCB = (void *)ProcGetProcessCB(data);
    *containerType = data->type;
    VnodeDrop();
    return processCB;
}

190 191
#endif /* LOSCFG_KERNEL_CONTAINER */

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
static int ProcessMemInfoRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
{
    unsigned int intSave;
    unsigned int size = sizeof(LosVmSpace) + sizeof(LosVmMapRegion);
    LosVmSpace *vmSpace = (LosVmSpace *)LOS_MemAlloc(m_aucSysMem1, size);
    if (vmSpace == NULL) {
        return -ENOMEM;
    }
    (void)memset_s(vmSpace, size, 0, size);
    LosVmMapRegion *heap = (LosVmMapRegion *)((char *)vmSpace + sizeof(LosVmSpace));

    SCHEDULER_LOCK(intSave);
    if (OsProcessIsInactive(pcb)) {
        SCHEDULER_UNLOCK(intSave);
        (void)LOS_MemFree(m_aucSysMem1, vmSpace);
        return -EINVAL;
    }
    (void)memcpy_s(vmSpace, sizeof(LosVmSpace), pcb->vmSpace, sizeof(LosVmSpace));
    (void)memcpy_s(heap, sizeof(LosVmMapRegion), pcb->vmSpace->heap, sizeof(LosVmMapRegion));
    SCHEDULER_UNLOCK(intSave);

Z
zhushengle 已提交
213 214 215 216
    (void)LosBufPrintf(seqBuf, "\nVMSpaceSize:      %u byte\n", vmSpace->size);
    (void)LosBufPrintf(seqBuf, "VMSpaceMapSize:   %u byte\n", vmSpace->mapSize);
    (void)LosBufPrintf(seqBuf, "VM TLB Asid:      %u\n", vmSpace->archMmu.asid);
    (void)LosBufPrintf(seqBuf, "VMHeapSize:       %u byte\n", heap->range.size);
Z
zhushengle 已提交
217
    (void)LosBufPrintf(seqBuf, "VMHeapRegionName: %s\n", OsGetRegionNameOrFilePath(heap));
Z
zhushengle 已提交
218
    (void)LosBufPrintf(seqBuf, "VMHeapRegionType: 0x%x\n", heap->regionType);
219 220 221 222 223
    (void)LOS_MemFree(m_aucSysMem1, vmSpace);
    return 0;
}

#ifdef LOSCFG_KERNEL_CPUP
Z
zhushengle 已提交
224
#define TIME_CYCLE_TO_US(time) ((((UINT64)time) * OS_NS_PER_CYCLE) / OS_SYS_NS_PER_US)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static int ProcessCpupRead(struct SeqBuf *seqBuf, LosProcessCB *pcb)
{
    unsigned int intSave;
    OsCpupBase *processCpup = (OsCpupBase *)LOS_MemAlloc(m_aucSysMem1, sizeof(OsCpupBase));
    if (processCpup == NULL) {
        return -ENOMEM;
    }
    (void)memset_s(processCpup, sizeof(OsCpupBase), 0, sizeof(OsCpupBase));

    SCHEDULER_LOCK(intSave);
    if (OsProcessIsInactive(pcb)) {
        SCHEDULER_UNLOCK(intSave);
        (VOID)LOS_MemFree(m_aucSysMem1, processCpup);
        return -EINVAL;
    }
    (void)memcpy_s(processCpup, sizeof(OsCpupBase), pcb->processCpup, sizeof(OsCpupBase));
    SCHEDULER_UNLOCK(intSave);

Z
zhushengle 已提交
243 244
    (void)LosBufPrintf(seqBuf, "\nTotalRunningTime: %lu us\n", TIME_CYCLE_TO_US(processCpup->allTime));
    (void)LosBufPrintf(seqBuf, "HistoricalRunningTime:(us) ");
245
    for (UINT32 i = 0; i < OS_CPUP_HISTORY_RECORD_NUM + 1; i++) {
Z
zhushengle 已提交
246
        (void)LosBufPrintf(seqBuf, "%lu  ", TIME_CYCLE_TO_US(processCpup->historyTime[i]));
247 248 249 250 251 252 253
    }
    (void)LosBufPrintf(seqBuf, "\n");
    (void)LOS_MemFree(m_aucSysMem1, processCpup);
    return 0;
}
#endif

Z
zhushengle 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
#ifdef LOSCFG_TIME_CONTAINER
static const CHAR *g_monotonic = "monotonic";
#define DECIMAL_BASE 10

static int ProcTimeContainerRead(struct SeqBuf *m, void *v)
{
    int ret;
    unsigned int intSave;
    struct timespec64 offsets = {0};

    if ((m == NULL) || (v == NULL)) {
        return -EINVAL;
    }

    struct ProcessData *data = (struct ProcessData *)v;
    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
270
    LosProcessCB *processCB = ProcGetProcessCB(data);
Z
zhushengle 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    ret = OsGetTimeContainerMonotonic(processCB, &offsets);
    SCHEDULER_UNLOCK(intSave);
    if (ret != LOS_OK) {
        return -ret;
    }

    LosBufPrintf(m, "monotonic %lld %ld\n", offsets.tv_sec, offsets.tv_nsec);
    return 0;
}

static int ProcSetTimensOffset(const char *buf, LosProcessCB *processCB)
{
    unsigned int intSave;
    struct timespec64 offsets;
    char *endptr = NULL;

    offsets.tv_sec = strtoll(buf, &endptr, DECIMAL_BASE);
Z
zhushengle 已提交
288
    offsets.tv_nsec = strtoll(endptr, NULL, DECIMAL_BASE);
Z
zhushengle 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 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 341 342
    if (offsets.tv_nsec >= OS_SYS_NS_PER_SECOND) {
        return -EACCES;
    }

    SCHEDULER_LOCK(intSave);
    unsigned int ret = OsSetTimeContainerMonotonic(processCB, &offsets);
    SCHEDULER_UNLOCK(intSave);
    if (ret != LOS_OK) {
        return -ret;
    }
    return 0;
}

static int ProcTimeContainerWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
{
    (void)ppos;
    char *kbuf = NULL;
    int ret;

    if ((pf == NULL) || (count <= 0)) {
        return -EINVAL;
    }

    struct ProcDirEntry *entry = pf->pPDE;
    if (entry == NULL) {
        return -EINVAL;
    }

    struct ProcessData *data = (struct ProcessData *)entry->data;
    if (data == NULL) {
        return -EINVAL;
    }

    if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)buf, count)) {
        kbuf = LOS_MemAlloc(m_aucSysMem1, count + 1);
        if (kbuf == NULL) {
            return -ENOMEM;
        }

        if (LOS_ArchCopyFromUser(kbuf, buf, count) != 0) {
            (VOID)LOS_MemFree(m_aucSysMem1, kbuf);
            return -EFAULT;
        }
        kbuf[count] = '\0';
        buf = kbuf;
    }

    ret = strncmp(buf, g_monotonic, strlen(g_monotonic));
    if (ret != 0) {
        (VOID)LOS_MemFree(m_aucSysMem1, kbuf);
        return -EINVAL;
    }

    buf += strlen(g_monotonic);
Z
zhushengle 已提交
343
    ret = ProcSetTimensOffset(buf, ProcGetProcessCB(data));
Z
zhushengle 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357
    if (ret < 0) {
        (VOID)LOS_MemFree(m_aucSysMem1, kbuf);
        return ret;
    }
    (VOID)LOS_MemFree(m_aucSysMem1, kbuf);
    return count;
}

static const struct ProcFileOperations TIME_CONTAINER_FOPS = {
    .read = ProcTimeContainerRead,
    .write = ProcTimeContainerWrite,
};
#endif

Z
zhushengle 已提交
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
#ifdef LOSCFG_USER_CONTAINER

static void *MemdupUserNul(const void *src, size_t len)
{
    char *des = NULL;
    if (len <= 0) {
        return NULL;
    }
    des = LOS_MemAlloc(OS_SYS_MEM_ADDR, len + 1);
    if (des == NULL) {
        return NULL;
    }

    if (LOS_ArchCopyFromUser(des, src, len) != 0) {
        (VOID)LOS_MemFree(OS_SYS_MEM_ADDR, des);
        return NULL;
    }

    des[len] = '\0';
    return des;
}

static LosProcessCB *ProcUidGidMapWriteCheck(struct ProcFile *pf, const char *buf, size_t size,
                                             char **kbuf, ProcessDataType *type)
{
    if ((pf == NULL) || (size <= 0) || (size >= PAGE_SIZE)) {
        return NULL;
    }

    struct ProcDirEntry *entry = pf->pPDE;
    if (entry == NULL) {
        return NULL;
    }

    struct ProcessData *data = (struct ProcessData *)entry->data;
    if (data == NULL) {
        return NULL;
    }

    *kbuf = MemdupUserNul(buf, size);
    if (*kbuf == NULL) {
        return NULL;
    }
    *type = (ProcessDataType)data->type;
    return ProcGetProcessCB(data);
}

static ssize_t ProcIDMapWrite(struct ProcFile *file, const char *buf, size_t size, loff_t *ppos)
{
    (void)ppos;
    char *kbuf = NULL;
    int ret;
    unsigned int intSave;
    ProcessDataType type = PROC_P_TYPE_MAX;
    LosProcessCB *processCB = ProcUidGidMapWriteCheck(file, buf, size, &kbuf, &type);
    if (processCB == NULL) {
        return -EINVAL;
    }

    SCHEDULER_LOCK(intSave);
    if ((processCB->credentials == NULL) || (processCB->credentials->userContainer == NULL)) {
        SCHEDULER_UNLOCK(intSave);
        (void)LOS_MemFree(m_aucSysMem1, kbuf);
        return -EINVAL;
    }
    UserContainer *userContainer = processCB->credentials->userContainer;
    if (userContainer->parent == NULL) {
        SCHEDULER_UNLOCK(intSave);
        (void)LOS_MemFree(m_aucSysMem1, kbuf);
        return -EPERM;
    }
    if (type == PROC_UID_MAP) {
        ret = OsUserContainerMapWrite(file, kbuf, size, CAP_SETUID,
                                      &userContainer->uidMap, &userContainer->parent->uidMap);
    } else {
        ret = OsUserContainerMapWrite(file, kbuf, size, CAP_SETGID,
                                      &userContainer->gidMap, &userContainer->parent->gidMap);
    }
    SCHEDULER_UNLOCK(intSave);
    (void)LOS_MemFree(m_aucSysMem1, kbuf);
    return ret;
}

static int ProcIDMapRead(struct SeqBuf *seqBuf, void *v)
{
    unsigned int intSave;
    if ((seqBuf == NULL) || (v == NULL)) {
        return -EINVAL;
    }
    struct ProcessData *data = (struct ProcessData *)v;
    LosProcessCB *processCB = ProcGetProcessCB(data);

    SCHEDULER_LOCK(intSave);
    if ((processCB->credentials == NULL) || (processCB->credentials->userContainer == NULL)) {
        SCHEDULER_UNLOCK(intSave);
        return -EINVAL;
    }
    UserContainer *userContainer = processCB->credentials->userContainer;
    if ((userContainer != NULL) && (userContainer->parent == NULL)) {
        UidGidExtent uidGidExtent = userContainer->uidMap.extent[0];
        SCHEDULER_UNLOCK(intSave);
        (void)LosBufPrintf(seqBuf, "%d %d %u\n", uidGidExtent.first, uidGidExtent.lowerFirst,
                           uidGidExtent.count);
        return 0;
    }
    SCHEDULER_LOCK(intSave);
    return 0;
}

static const struct ProcFileOperations UID_GID_MAP_FOPS = {
    .read = ProcIDMapRead,
    .write = ProcIDMapWrite,
};
#endif

473 474
static int ProcProcessRead(struct SeqBuf *m, void *v)
{
475 476 477 478 479 480
    if ((m == NULL) || (v == NULL)) {
        return -EINVAL;
    }
    struct ProcessData *data = (struct ProcessData *)v;
    switch (data->type) {
        case PROC_PID_MEM:
Z
zhushengle 已提交
481
            return ProcessMemInfoRead(m, ProcGetProcessCB(data));
482 483
#ifdef LOSCFG_KERNEL_CPUP
        case PROC_PID_CPUP:
Z
zhushengle 已提交
484
            return ProcessCpupRead(m, ProcGetProcessCB(data));
485 486 487 488
#endif
        default:
            break;
    }
489 490 491 492 493 494 495 496 497 498
    return -EINVAL;
}

static const struct ProcFileOperations PID_FOPS = {
    .read = ProcProcessRead,
};

static struct ProcProcess g_procProcess[] = {
    {
        .name = NULL,
Z
zhushengle 已提交
499
        .mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IROTH,
500 501 502 503
        .type = PROC_PID,
        .fileOps = &PID_FOPS

    },
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    {
        .name = "meminfo",
        .mode = 0,
        .type = PROC_PID_MEM,
        .fileOps = &PID_FOPS
    },
#ifdef LOSCFG_KERNEL_CPUP
    {
        .name = "cpup",
        .mode = 0,
        .type = PROC_PID_CPUP,
        .fileOps = &PID_FOPS

    },
#endif
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
#ifdef LOSCFG_KERNEL_CONTAINER
    {
        .name = "container",
        .mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH,
        .type = CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS

    },
#ifdef LOSCFG_PID_CONTAINER
    {
        .name = "container/pid",
        .mode = S_IFLNK,
        .type = PID_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
Z
zhushengle 已提交
534 535 536 537 538 539
    {
        .name = "container/pid_for_children",
        .mode = S_IFLNK,
        .type = PID_CHILD_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
540 541 542 543 544 545 546 547 548
#endif
#ifdef LOSCFG_UTS_CONTAINER
    {
        .name = "container/uts",
        .mode = S_IFLNK,
        .type = UTS_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
#endif
549 550 551 552 553 554 555 556
#ifdef LOSCFG_MNT_CONTAINER
    {
        .name = "container/mnt",
        .mode = S_IFLNK,
        .type = MNT_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
#endif
Z
zhushengle 已提交
557 558 559 560 561 562 563 564
#ifdef LOSCFG_IPC_CONTAINER
    {
        .name = "container/ipc",
        .mode = S_IFLNK,
        .type = IPC_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
#endif
Z
zhushengle 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
#ifdef LOSCFG_TIME_CONTAINER
    {
        .name = "container/time",
        .mode = S_IFLNK,
        .type = TIME_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
    {
        .name = "container/time_for_children",
        .mode = S_IFLNK,
        .type = TIME_CHILD_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
    {
        .name = "time_offsets",
        .mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
        .type = TIME_CONTAINER,
        .fileOps = &TIME_CONTAINER_FOPS
    },
#endif
Z
zhushengle 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
#ifdef LOSCFG_IPC_CONTAINER
    {
        .name = "container/user",
        .mode = S_IFLNK,
        .type = USER_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
    {
        .name = "uid_map",
        .mode = 0,
        .type = PROC_UID_MAP,
        .fileOps = &UID_GID_MAP_FOPS
    },
    {
        .name = "gid_map",
        .mode = 0,
        .type = PROC_GID_MAP,
        .fileOps = &UID_GID_MAP_FOPS
    },
#endif
Z
zhushengle 已提交
605 606 607 608 609 610 611 612
#ifdef LOSCFG_IPC_CONTAINER
    {
        .name = "container/net",
        .mode = S_IFLNK,
        .type = NET_CONTAINER,
        .fileOps = &PID_CONTAINER_FOPS
    },
#endif
613 614 615 616 617 618 619 620 621 622 623 624 625 626
#endif
};

void ProcFreeProcessDir(struct ProcDirEntry *processDir)
{
    if (processDir == NULL) {
        return;
    }
    RemoveProcEntry(processDir->name, NULL);
}

static struct ProcDirEntry *ProcCreatePorcess(UINT32 pid, struct ProcProcess *porcess, uintptr_t processCB)
{
    int ret;
Z
zhushengle 已提交
627
    struct ProcDataParm dataParm;
628 629 630 631 632
    char pidName[PROC_PID_DIR_LEN] = {0};
    struct ProcessData *data = (struct ProcessData *)malloc(sizeof(struct ProcessData));
    if (data == NULL) {
        return NULL;
    }
Z
zhushengle 已提交
633 634 635 636 637 638
    if (pid != OS_INVALID_VALUE) {
        if (porcess->name != NULL) {
            ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%u/%s", pid, porcess->name);
        } else {
            ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%u", pid);
        }
639
    } else {
Z
zhushengle 已提交
640 641 642 643 644
        if (porcess->name != NULL) {
            ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%s/%s", "self", porcess->name);
        } else {
            ret = snprintf_s(pidName, PROC_PID_DIR_LEN, PROC_PID_DIR_LEN - 1, "%s", "self");
        }
645 646 647 648 649 650 651 652
    }
    if (ret < 0) {
        free(data);
        return NULL;
    }

    data->process = processCB;
    data->type = porcess->type;
Z
zhushengle 已提交
653 654 655
    dataParm.data = data;
    dataParm.dataType = PROC_DATA_FREE;
    struct ProcDirEntry *container = ProcCreateData(pidName, porcess->mode, NULL, porcess->fileOps, &dataParm);
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    if (container == NULL) {
        free(data);
        PRINT_ERR("create /proc/%s error!\n", pidName);
        return NULL;
    }
    return container;
}

int ProcCreateProcessDir(UINT32 pid, uintptr_t process)
{
    unsigned int intSave;
    struct ProcDirEntry *pidDir = NULL;
    for (int index = 0; index < (sizeof(g_procProcess) / sizeof(struct ProcProcess)); index++) {
        struct ProcProcess *procProcess = &g_procProcess[index];
        struct ProcDirEntry *dir = ProcCreatePorcess(pid, procProcess, process);
        if (dir == NULL) {
            PRINT_ERR("create /proc/%s error!\n", procProcess->name);
            goto CREATE_ERROR;
        }
        if (index == 0) {
            pidDir = dir;
        }
    }

Z
zhushengle 已提交
680 681 682 683 684 685
    if (process != 0) {
        SCHEDULER_LOCK(intSave);
        ((LosProcessCB *)process)->procDir = pidDir;
        SCHEDULER_UNLOCK(intSave);
    }

686 687 688 689 690 691 692 693 694 695
    return 0;

CREATE_ERROR:
    if (pidDir != NULL) {
        RemoveProcEntry(pidDir->name, NULL);
    }
    return -1;
}
#endif /* LOSCFG_PROC_PROCESS_DIR */

M
mamingshuai 已提交
696 697 698 699 700 701
static int ProcessProcFill(struct SeqBuf *m, void *v)
{
    (void)v;
    (void)OsShellCmdTskInfoGet(OS_ALL_TASK_MASK, m, OS_PROCESS_INFO_ALL);
    return 0;
}
W
wenjun 已提交
702

M
mamingshuai 已提交
703 704 705
static const struct ProcFileOperations PROCESS_PROC_FOPS = {
    .read       = ProcessProcFill,
};
W
wenjun 已提交
706

M
mamingshuai 已提交
707 708 709 710 711 712 713 714
void ProcProcessInit(void)
{
    struct ProcDirEntry *pde = CreateProcEntry("process", 0, NULL);
    if (pde == NULL) {
        PRINT_ERR("create /proc/process error!\n");
        return;
    }
    pde->procFileOps = &PROCESS_PROC_FOPS;
W
wenjun 已提交
715

716
#ifdef LOSCFG_PROC_PROCESS_DIR
Z
zhushengle 已提交
717 718 719 720 721 722
    int ret = ProcCreateProcessDir(OS_INVALID_VALUE, 0);
    if (ret < 0) {
        PRINT_ERR("Create proc process self dir failed!\n");
    }

    ret = ProcCreateProcessDir(OS_USER_ROOT_PROCESS_ID, (uintptr_t)OsGetUserInitProcess());
723 724 725 726 727 728
    if (ret < 0) {
        PRINT_ERR("Create proc process %d dir failed!\n", OS_USER_ROOT_PROCESS_ID);
    }
#endif
    return;
}