File indexing completed on 2024-04-14 15:50:45

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 <qtcurve-utils/log.h>
0024 #include <assert.h>
0025 #include <unistd.h>
0026 #include <time.h>
0027 #include <sys/socket.h>
0028 
0029 static char buff1[1024];
0030 static char buff2[1024];
0031 
0032 static void
0033 fillBuffer(char *buff, size_t size)
0034 {
0035     for (unsigned i = 0;i < size - 1;i++) {
0036         while (!buff[i]) {
0037             buff[i] = random();
0038         }
0039     }
0040     buff[size - 1] = '\0';
0041 }
0042 
0043 static void
0044 subProcess(int, char **argv)
0045 {
0046     char buff3[1024] = {0};
0047     read(0, buff3, sizeof(buff3));
0048     assert(memcmp(argv[1], buff3, sizeof(buff3)) == 0);
0049     write(1, argv[2], strlen(argv[2]) + 1);
0050 }
0051 
0052 static void
0053 mainProcess(int, char **argv)
0054 {
0055     srandom(time(nullptr));
0056     fillBuffer(buff1, sizeof(buff1));
0057     fillBuffer(buff2, sizeof(buff2));
0058     QtcPopenBuff popen_buffs[] = {{
0059             .orig = 0,
0060             .mode = QTC_POPEN_WRITE,
0061             .buff = buff1,
0062             .len = sizeof(buff1)
0063         }, {
0064             .orig = 1,
0065             .mode = QTC_POPEN_READ,
0066             .buff = (char*)malloc(10),
0067             .len = 0
0068         }
0069     };
0070     alarm(1);
0071     const char* const args[] = {argv[0], buff1, buff2, nullptr};
0072     qtcPopenBuff(argv[0], args, sizeof(popen_buffs) / sizeof(popen_buffs[0]),
0073                  popen_buffs, 100);
0074     assert(popen_buffs[1].len == sizeof(buff2));
0075     assert(memcmp(buff2, popen_buffs[1].buff, sizeof(buff2)) == 0);
0076     free(popen_buffs[1].buff);
0077 }
0078 
0079 int
0080 main(int argc, char **argv)
0081 {
0082     if (argv[1]) {
0083         subProcess(argc, argv);
0084     } else {
0085         mainProcess(argc, argv);
0086     }
0087     return 0;
0088 }