1 /* 2 * A resource offering support for creating processes. 3 * 4 * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <systypes/fcntl.h> 23 24 #include "process_creator_resource.h" 25 #include "process_creator_context_resource.h" 26 #include "process_creator_context_object_server.h" 27 28 #include <string.h> 29 #include <stdio.h> 30 31 32 33 /* Support for creating processes. */ 34 35 ProcessCreatorContextResource::ProcessCreatorContextResource(ProcessCreatorResource *creator) 36 : SimplePager(), _creator(creator) 37 { 38 } 39 40 ProcessCreatorContextResource::~ProcessCreatorContextResource() 41 { 42 } 43 44 ipc_server_default_config_type ProcessCreatorContextResource::config() 45 { 46 return config_ProcessCreatorContextObject; 47 } 48 49 50 51 /* ProcessCreatorContext interface methods. */ 52 53 long ProcessCreatorContextResource::start(int argc, l4_cap_idx_t reader, 54 l4_cap_idx_t writer, 55 l4_cap_idx_t *process) 56 { 57 /* Obtain the arguments by reading from the shared memory. */ 58 59 const char *argv[argc]; 60 offset_t pos = 0; 61 62 if (!argc) 63 return -L4_EINVAL; 64 65 for (int i = 0; i < argc; i++) 66 { 67 argv[i] = get_string(pos); 68 69 if (argv[i] != NULL) 70 pos += strlen(argv[i]) + 1; 71 72 printf("argv[%d] = %s\n", i, argv[i]); 73 } 74 75 /* Obtain a reference to a new process and send the reference to the client. 76 This must be done in advance of actually starting the program since the 77 program may complete before the client gets the reference. 78 79 The reference needs to be shared and its reference count increased to 80 prevent the closure of the process monitor. */ 81 82 long err = _creator->init_process(process); 83 84 if (!err) 85 err = complete_ProcessCreatorContext_start(*process); 86 87 /* Attempt to communicate any error from these activities. */ 88 89 if (err) 90 return err; 91 92 /* Attempt to actually start the process. Errors cannot be communicated as a 93 reply, so a notification is sent via the process monitor instead by the 94 process creator. */ 95 96 _creator->start(argc, argv, reader, writer, *process); 97 98 return IPC_MESSAGE_SENT; 99 } 100 101 /* vim: tabstop=4 expandtab shiftwidth=4 102 */