1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #if !defined(__NET_KSFT_H__) 3 #define __NET_KSFT_H__ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <unistd.h> 8 9 static inline void ksft_ready(void) 10 { 11 const char msg[7] = "ready\n"; 12 char *env_str; 13 int fd; 14 15 env_str = getenv("KSFT_READY_FD"); 16 if (env_str) { 17 fd = atoi(env_str); 18 if (!fd) { 19 fprintf(stderr, "invalid KSFT_READY_FD = '%s'\n", 20 env_str); 21 return; 22 } 23 } else { 24 fd = STDOUT_FILENO; 25 } 26 27 if (write(fd, msg, sizeof(msg)) < 0) 28 perror("write()"); 29 if (fd != STDOUT_FILENO) 30 close(fd); 31 } 32 33 static inline void ksft_wait(void) 34 { 35 char *env_str; 36 char byte; 37 int fd; 38 39 env_str = getenv("KSFT_WAIT_FD"); 40 if (env_str) { 41 fd = atoi(env_str); 42 if (!fd) { 43 fprintf(stderr, "invalid KSFT_WAIT_FD = '%s'\n", 44 env_str); 45 return; 46 } 47 } else { 48 /* Not running in KSFT env, wait for input from STDIN instead */ 49 fd = STDIN_FILENO; 50 } 51 52 if (read(fd, &byte, sizeof(byte)) < 0) 53 perror("read()"); 54 if (fd != STDIN_FILENO) 55 close(fd); 56 } 57 58 #endif 59