File indexing completed on 2024-03-24 17:24:18
0001 /***************************************************************************** 0002 * Copyright 2014 - 2015 Yichao Yu <yyc1992@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU Lesser General Public License as * 0006 * published by the Free Software Foundation; either version 2.1 of the * 0007 * License, or (at your option) version 3, or any later version accepted * 0008 * by the membership of KDE e.V. (or its successor approved by the * 0009 * membership of KDE e.V.), which shall act as a proxy defined in * 0010 * Section 6 of version 3 of the license. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0015 * Lesser General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU Lesser General Public * 0018 * License along with this library. If not, * 0019 * see <http://www.gnu.org/licenses/>. * 0020 *****************************************************************************/ 0021 0022 #include <qtcurve-utils/process.h> 0023 #include <assert.h> 0024 #include <unistd.h> 0025 #include <time.h> 0026 #include <sys/socket.h> 0027 0028 static char buff1[1024]; 0029 static char buff2[1024]; 0030 0031 static void 0032 fillBuffer(char *buff, size_t size) 0033 { 0034 for (unsigned i = 0;i < size - 1;i++) { 0035 while (!buff[i]) { 0036 buff[i] = random(); 0037 } 0038 } 0039 buff[size - 1] = '\0'; 0040 } 0041 0042 static void 0043 subProcess(int, char **argv) 0044 { 0045 char buff3[1024] = {0}; 0046 read(0, buff3, sizeof(buff3)); 0047 assert(memcmp(argv[1], buff3, sizeof(buff3)) == 0); 0048 write(1, argv[2], strlen(argv[2]) + 1); 0049 } 0050 0051 static void 0052 mainProcess(int, char **argv) 0053 { 0054 srandom(time(nullptr)); 0055 fillBuffer(buff1, sizeof(buff1)); 0056 fillBuffer(buff2, sizeof(buff2)); 0057 QtcPopenFD fds[] = {{ 0058 .orig = 0, 0059 .replace = 0, 0060 .mode = QTC_POPEN_WRITE 0061 }, { 0062 .orig = 1, 0063 .replace = 0, 0064 .mode = QTC_POPEN_READ 0065 } 0066 }; 0067 alarm(1); 0068 const char* const args[] = {argv[0], buff1, buff2, nullptr}; 0069 qtcPopen(argv[0], args, sizeof(fds) / sizeof(fds[0]), fds); 0070 write(fds[0].replace, buff1, sizeof(buff1)); 0071 shutdown(fds[0].replace, SHUT_RDWR); 0072 close(fds[0].replace); 0073 char buff3[1024] = {0}; 0074 read(fds[1].replace, buff3, sizeof(buff3)); 0075 assert(memcmp(buff2, buff3, sizeof(buff3)) == 0); 0076 } 0077 0078 int 0079 main(int argc, char **argv) 0080 { 0081 if (argv[1]) { 0082 subProcess(argc, argv); 0083 } else { 0084 mainProcess(argc, argv); 0085 } 0086 return 0; 0087 }