| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <QSharedMemory> | 
| 30 | #include <QStringList> | 
| 31 | #include <QDebug> | 
| 32 | #include <QTest> | 
| 33 | #include <stdio.h> | 
| 34 |  | 
| 35 | void set(QSharedMemory &sm, int pos, QChar value) | 
| 36 | { | 
| 37 |     ((char*)sm.data())[pos] = value.toLatin1(); | 
| 38 | } | 
| 39 |  | 
| 40 | QChar get(QSharedMemory &sm, int i) | 
| 41 | { | 
| 42 |     return QChar::fromLatin1(c: ((char*)sm.data())[i]); | 
| 43 | } | 
| 44 |  | 
| 45 | int readonly_segfault() | 
| 46 | { | 
| 47 |     QSharedMemory sharedMemory; | 
| 48 |     sharedMemory.setKey("readonly_segfault" ); | 
| 49 |     sharedMemory.create(size: 1024, mode: QSharedMemory::ReadOnly); | 
| 50 |     sharedMemory.lock(); | 
| 51 |     set(sm&: sharedMemory, pos: 0, value: 'a'); | 
| 52 |     sharedMemory.unlock(); | 
| 53 |     return EXIT_SUCCESS; | 
| 54 | } | 
| 55 |  | 
| 56 | int producer() | 
| 57 | { | 
| 58 |     QSharedMemory producer; | 
| 59 |     producer.setKey("market" ); | 
| 60 |  | 
| 61 |     int size = 1024; | 
| 62 |     if (!producer.create(size)) { | 
| 63 |         if (producer.error() == QSharedMemory::AlreadyExists) { | 
| 64 |             if (!producer.attach()) { | 
| 65 |                 qWarning() << "Could not attach to"  << producer.key(); | 
| 66 |                 return EXIT_FAILURE; | 
| 67 |             } | 
| 68 |         } else { | 
| 69 |             qWarning() << "Could not create"  << producer.key(); | 
| 70 |             return EXIT_FAILURE; | 
| 71 |         } | 
| 72 |     } | 
| 73 |     // tell parent we're ready | 
| 74 |     //qDebug("producer created and attached"); | 
| 75 |     puts(s: "" ); | 
| 76 |     fflush(stdout); | 
| 77 |  | 
| 78 |     if (!producer.lock()) { | 
| 79 |         qWarning() << "Could not lock"  << producer.key(); | 
| 80 |         return EXIT_FAILURE; | 
| 81 |     } | 
| 82 |     set(sm&: producer, pos: 0, value: 'Q'); | 
| 83 |     if (!producer.unlock()) { | 
| 84 |         qWarning() << "Could not lock"  << producer.key(); | 
| 85 |         return EXIT_FAILURE; | 
| 86 |     } | 
| 87 |  | 
| 88 |     int i = 0; | 
| 89 |     while (i < 5) { | 
| 90 |         if (!producer.lock()) { | 
| 91 |             qWarning() << "Could not lock"  << producer.key(); | 
| 92 |             return EXIT_FAILURE; | 
| 93 |         } | 
| 94 |         if (get(sm&: producer, i: 0) == 'Q') { | 
| 95 |             if (!producer.unlock()) { | 
| 96 |                 qWarning() << "Could not unlock"  << producer.key(); | 
| 97 |                 return EXIT_FAILURE; | 
| 98 |             } | 
| 99 |             QTest::qSleep(ms: 1); | 
| 100 |             continue; | 
| 101 |         } | 
| 102 |         //qDebug() << "producer:" << i); | 
| 103 |         ++i; | 
| 104 |         set(sm&: producer, pos: 0, value: 'Q'); | 
| 105 |         if (!producer.unlock()) { | 
| 106 |             qWarning() << "Could not unlock"  << producer.key(); | 
| 107 |             return EXIT_FAILURE; | 
| 108 |         } | 
| 109 |         QTest::qSleep(ms: 1); | 
| 110 |     } | 
| 111 |     if (!producer.lock()) { | 
| 112 |         qWarning() << "Could not lock"  << producer.key(); | 
| 113 |         return EXIT_FAILURE; | 
| 114 |     } | 
| 115 |     set(sm&: producer, pos: 0, value: 'E'); | 
| 116 |     if (!producer.unlock()) { | 
| 117 |         qWarning() << "Could not unlock"  << producer.key(); | 
| 118 |         return EXIT_FAILURE; | 
| 119 |     } | 
| 120 |  | 
| 121 |     //qDebug("producer done"); | 
| 122 |  | 
| 123 |     // Sleep for a bit to let all consumers exit | 
| 124 |     getchar(); | 
| 125 |     return EXIT_SUCCESS; | 
| 126 | } | 
| 127 |  | 
| 128 | int consumer() | 
| 129 | { | 
| 130 |     QSharedMemory consumer; | 
| 131 |     consumer.setKey("market" ); | 
| 132 |  | 
| 133 |     //qDebug("consumer starting"); | 
| 134 |     int tries = 0; | 
| 135 |     while (!consumer.attach()) { | 
| 136 |         if (tries == 5000) { | 
| 137 |             qWarning() << "consumer exiting, waiting too long" ; | 
| 138 |             return EXIT_FAILURE; | 
| 139 |         } | 
| 140 |         ++tries; | 
| 141 |         QTest::qSleep(ms: 1); | 
| 142 |     } | 
| 143 |     //qDebug("consumer attached"); | 
| 144 |  | 
| 145 |  | 
| 146 |     int i = 0; | 
| 147 |     while (true) { | 
| 148 |         if (!consumer.lock()) { | 
| 149 |             qWarning() << "Could not lock"  << consumer.key(); | 
| 150 |             return EXIT_FAILURE; | 
| 151 |         } | 
| 152 |         if (get(sm&: consumer, i: 0) == 'Q') { | 
| 153 |             set(sm&: consumer, pos: 0, value: ++i); | 
| 154 |             //qDebug() << "consumer sets" << i; | 
| 155 |         } | 
| 156 |         if (get(sm&: consumer, i: 0) == 'E') { | 
| 157 |             if (!consumer.unlock()) { | 
| 158 |                 qWarning() << "Could not unlock"  << consumer.key(); | 
| 159 |                 return EXIT_FAILURE; | 
| 160 |             } | 
| 161 |             break; | 
| 162 |         } | 
| 163 |         if (!consumer.unlock()) { | 
| 164 |             qWarning() << "Could not unlock"  << consumer.key(); | 
| 165 |             return EXIT_FAILURE; | 
| 166 |         } | 
| 167 |         QTest::qSleep(ms: 10); | 
| 168 |     } | 
| 169 |  | 
| 170 |     //qDebug("consumer detaching"); | 
| 171 |     if (!consumer.detach()) { | 
| 172 |         qWarning() << "Could not detach"  << consumer.key(); | 
| 173 |         return EXIT_FAILURE; | 
| 174 |     } | 
| 175 |     return EXIT_SUCCESS; | 
| 176 | } | 
| 177 |  | 
| 178 | int main(int argc, char *argv[]) | 
| 179 | { | 
| 180 |     QCoreApplication app(argc, argv); | 
| 181 |  | 
| 182 |     QStringList arguments = app.arguments(); | 
| 183 |     if (app.arguments().count() != 2) { | 
| 184 |         qWarning(msg: "Please call the helper with the function to call as argument" ); | 
| 185 |         return EXIT_FAILURE; | 
| 186 |     } | 
| 187 |     QString function = arguments.at(i: 1); | 
| 188 |     if (function == QLatin1String("readonly_segfault" )) | 
| 189 |         return readonly_segfault(); | 
| 190 |     else if (function == QLatin1String("producer" )) | 
| 191 |         return producer(); | 
| 192 |     else if (function == QLatin1String("consumer" )) | 
| 193 |         return consumer(); | 
| 194 |     else | 
| 195 |         qWarning() << "Unknown function"  << arguments.at(i: 1); | 
| 196 |  | 
| 197 |     return EXIT_SUCCESS; | 
| 198 | } | 
| 199 |  |