• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include <assert.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <pthread.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include <chcore/launcher.h>
23 #include <chcore/defs.h>
24 #include <chcore/ipc.h>
25 #include <chcore-internal/procmgr_defs.h>
26 #include <chcore/syscall.h>
27 #include <chcore/pthread.h>
28 
29 #include <spawn_ext.h>
30 
__info_proc_by_pid(pid_t pid,struct proc_info * info)31 static int __info_proc_by_pid(pid_t pid, struct proc_info *info)
32 {
33     ipc_msg_t *ipc_msg;
34     struct proc_request *req;
35     int ret;
36 
37     ipc_msg = ipc_create_msg(procmgr_ipc_struct, sizeof(struct proc_request));
38     req = (struct proc_request *)ipc_get_msg_data(ipc_msg);
39     req->req = PROC_REQ_INFO_PROC_BY_PID;
40 
41     req->info_proc_by_pid.pid = pid;
42     ret = ipc_call(procmgr_ipc_struct, ipc_msg);
43 
44     if (ret != 0) {
45         goto out;
46     }
47 
48     memcpy(info, &req->info_proc_by_pid.info, sizeof(*info));
49 
50 out:
51     ipc_destroy_msg(ipc_msg);
52     return ret;
53 }
54 
getuuid(pid_t pid,spawn_uuid_t * uuid)55 int getuuid(pid_t pid, spawn_uuid_t *uuid)
56 {
57     struct proc_info info;
58     int ret;
59 
60     ret = __info_proc_by_pid(pid, &info);
61     if (ret == 0) {
62         memcpy(uuid, &info.uuid, sizeof(*uuid));
63     }
64 
65     return ret;
66 }
67 
68 #define DEFAULT_STACK_SIZE 0x800000UL
69 #define DEFAULT_HEAP_SIZE  0x8000000UL
70 
spawnattr_init(posix_spawnattr_t * attr)71 int spawnattr_init(posix_spawnattr_t *attr)
72 {
73     *attr = (posix_spawnattr_t){0};
74     attr->stack_size = DEFAULT_STACK_SIZE;
75     attr->heap_size = DEFAULT_HEAP_SIZE;
76     return 0;
77 }
78 
spawnattr_setuuid(posix_spawnattr_t * attr,const spawn_uuid_t * uuid)79 void spawnattr_setuuid(posix_spawnattr_t *attr, const spawn_uuid_t *uuid)
80 {
81     attr->uuid = *uuid;
82 }
83 
spawnattr_setheap(posix_spawnattr_t * attr,size_t size)84 int spawnattr_setheap(posix_spawnattr_t *attr, size_t size)
85 {
86     attr->heap_size = size;
87     return 0;
88 }
89 
spawnattr_setstack(posix_spawnattr_t * attr,size_t size)90 int spawnattr_setstack(posix_spawnattr_t *attr, size_t size)
91 {
92     attr->stack_size = size;
93     return 0;
94 }
95 
thread_terminate(pthread_t thread)96 int32_t thread_terminate(pthread_t thread)
97 {
98     int tid, ret;
99 
100     tid = chcore_pthread_get_tid(thread);
101     ret = usys_terminate_thread(tid);
102 
103     chcore_pthread_wake_joiner(thread);
104 
105     return ret;
106 }
107 
posix_spawn_ex(pid_t * pid,const char * path,const posix_spawn_file_actions_t * file_action,const posix_spawnattr_t * attrp,char ** argv,char ** envp,int * tid)108 int posix_spawn_ex(pid_t *pid, const char *path,
109                    const posix_spawn_file_actions_t *file_action,
110                    const posix_spawnattr_t *attrp, char **argv, char **envp,
111                    int *tid)
112 {
113     int ret;
114     int argc = 0;
115 
116     if (argv != NULL) {
117         while (argv[argc] != NULL) {
118             argc++;
119         }
120     }
121 
122     ret = chcore_new_process_spawn(argc, argv, envp, attrp, tid, path);
123     if (ret >= 0) {
124         if (pid != NULL) {
125             *pid = ret;
126         }
127         ret = 0;
128     }
129     return ret;
130 }
131 
getstacksize(void)132 size_t getstacksize(void)
133 {
134     struct proc_info info;
135     int ret;
136 
137     ret = __info_proc_by_pid(getpid(), &info);
138     if (ret == 0) {
139         return info.stack_size;
140     }
141 
142     return 0;
143 }
144