GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — SKRoot-linuxKernelRoot/testRoot/safe_fork_helper.h at master · xdcs100/SKRoot-linuxKernelRoot Expand file tree/ Copy pathsafe_fork_helper.hMore file actions 246 lines (219 loc) · 5.3 KB / Copy pathsafe_fork_helper.hFile metadata and controls246 lines (219 loc) · 5.3 KB 1 2 3 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 34 35 36 37 38 39 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 #ifndef _SAFE_FORK_HELPER_H_ #define _SAFE_FORK_HELPER_H_ #include <string.h> #include <iostream> #include <fstream> #include <memory> #include <vector> #include <sys/stat.h> #include <sys/types.h> #include <sys/socket.h> class fork_base_info { public: fork_base_info() { reset(); } ~fork_base_info() { close(); } int fd_read = -1; int fd_write = -1; pid_t pid = 0; void reset() { close(); fd_read = -1; fd_write = -1; pid = 0; } void close() { if (fd_read != -1) { ::close(fd_read); fd_read = -1; } if (fd_write != -1) { ::close(fd_write); fd_write = -1; } } }; class fork_pipe_info : public fork_base_info {}; static bool fork_pipe_child_process(fork_pipe_info & finfo) { int fd[2]; if (pipe(fd)) { return false; } pid_t pid; if ((pid = fork()) < 0) { //fork error return false; } finfo.pid = pid; if(pid == 0) { // child process close(fd[0]); //close read pipe finfo.fd_read = -1; finfo.fd_write = fd[1]; return true; } // father process close(fd[1]); //close write pipe finfo.fd_read = fd[0]; finfo.fd_write = -1; return false; } static bool wait_fork_child_process(const fork_base_info & finfo) { if(finfo.pid == 0) { return false; } int status; if (waitpid(finfo.pid, &status, WUNTRACED) < 0) { return false; } return true; } static bool write_errcode_to_father(const fork_pipe_info & finfo, ssize_t errCode) { if(write(finfo.fd_write, &errCode, sizeof(errCode))==sizeof(errCode)) { return true; } return false; } static bool read_errcode_from_child(const fork_pipe_info & finfo, ssize_t & errCode) { if(read(finfo.fd_read, (void*)&errCode, sizeof(errCode))==sizeof(errCode)) { return true; } return false; } static bool write_int_to_father(const fork_pipe_info & finfo, int n) { if(write(finfo.fd_write, &n, sizeof(n))==sizeof(n)) { return true; } return false; } static bool read_int_from_child(const fork_pipe_info & finfo, int & n) { if(read(finfo.fd_read, (void*)&n, sizeof(n))==sizeof(n)) { return true; } return false; } static bool write_vec_int_to_father(const fork_pipe_info & finfo, const std::vector<int> & v) { size_t len = v.size(); if(write(finfo.fd_write, &len, sizeof(len))!=sizeof(len)) { return false; } for(int i : v) { if(write(finfo.fd_write, &i, sizeof(i))!=sizeof(i)) { return false; } } return true; } static bool read_vec_int_from_child(const fork_pipe_info & finfo, std::vector<int> & v) { size_t len = 0; if(read(finfo.fd_read, (void*)&len, sizeof(len))!=sizeof(len)) { return false; } for(size_t i = 0; i < len; i++) { int n = 0; if(read(finfo.fd_read, (void*)&n, sizeof(n))!=sizeof(n)) { return false; } v.push_back(n); } return true; } static bool write_string_to_father(const fork_pipe_info & finfo, const std::string &text) { size_t len = text.length(); if(write(finfo.fd_write, &len, sizeof(len))!=sizeof(len)) { return false; } if(write(finfo.fd_write, text.c_str(), len)!=len) { return false; } return true; } static bool read_string_from_child(const fork_pipe_info & finfo, std::string &text) { size_t len = 0; if(read(finfo.fd_read, (void*)&len, sizeof(len))!=sizeof(len)) { return false; } std::shared_ptr<char> sp_buf(new (std::nothrow) char[len], std::default_delete<char[]>()); if (!sp_buf) { return false; } if(read(finfo.fd_read, sp_buf.get(), len)!=len) { return false; } text.assign(sp_buf.get(), len); return true; } class fork_socketpair_info : public fork_pipe_info {}; static bool fork_socketpair_child_process(fork_socketpair_info & finfo) { int fd[2]; if (socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) < 0) { return false; } pid_t pid; if ((pid = fork()) < 0) { //fork error return false; } finfo.pid = pid; if(pid == 0) { // child process close(fd[0]); //close read pipe finfo.fd_read = -1; finfo.fd_write = fd[1]; return true; } // father process close(fd[1]); //close write pipe finfo.fd_read = fd[0]; finfo.fd_write = -1; return false; } static bool write_fd_to_father(const fork_socketpair_info & finfo, int fd) { iovec iov[1]; msghdr msg; char buff[0]; //指定缓冲区 iov[0].iov_base = buff; iov[0].iov_len = 1; //通过socketpair进行通信,不需要知道ip地址 msg.msg_name = nullptr; msg.msg_namelen = 0; //指定内存缓冲区 msg.msg_iov = iov; msg.msg_iovlen = 1; //辅助数据 cmsghdr cm; cm.cmsg_len = CMSG_LEN(sizeof(fd)); //描述符的大小 cm.cmsg_level = SOL_SOCKET; //表示传递的辅助数据是文件描述符 cm.cmsg_type = SCM_RIGHTS; //协议类型 *(int*)CMSG_DATA(&cm) = fd; //设置待发送描述符 //设置辅助数据 msg.msg_control = &cm; msg.msg_controllen = CMSG_LEN(sizeof(fd)); if(sendmsg(finfo.fd_write, &msg, 0) == -1) { return false; } return true; } static bool read_fd_from_child(const fork_socketpair_info & finfo, int & fd) { iovec iov[1]; msghdr msg; char buff[0]; //指定缓冲区 iov[0].iov_base = buff; iov[0].iov_len = 1; //通过socketpair进行通信,不需要知道ip地址 msg.msg_name = nullptr; msg.msg_namelen = 0; //指定内存缓冲区 msg.msg_iov = iov; msg.msg_iovlen = 1; //辅助数据 cmsghdr cm; //设置辅助数据 msg.msg_control = &cm; msg.msg_controllen = CMSG_LEN(sizeof(fd)); if(recvmsg(finfo.fd_read, &msg, 0) == -1) { return false; } fd = *(int*)CMSG_DATA(&cm); return true; } #endif /* _SAFE_FORK_HELPER_H_ */ |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |