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 QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qglobal.h"
41 
42 #ifdef Q_OS_VXWORKS
43 
44 #include "qplatformdefs.h"
45 #include "qfunctions_vxworks.h"
46 
47 #if defined(_WRS_KERNEL)
48 #include <vmLib.h>
49 #endif
50 #include <selectLib.h>
51 #include <ioLib.h>
52 
53 QT_USE_NAMESPACE
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 // no lfind() - used by the TIF image format
60 void *lfind(const void* key, const void* base, size_t* elements, size_t size,
61             int (*compare)(const void*, const void*))
62 {
63     const char* current = (char*) base;
64     const char* const end = (char*) (current + (*elements) * size);
65     while (current != end) {
66         if (compare(current, key) == 0)
67             return (void*)current;
68         current += size;
69     }
70     return 0;
71 }
72 
73 
74 // no rand_r(), but rand()
75 // NOTE: this implementation is wrong for multi threaded applications,
76 // but there is no way to get it right on VxWorks (in kernel mode)
77 #if defined(_WRS_KERNEL)
78 int rand_r(unsigned int * /*seedp*/)
79 {
80     return rand();
81 }
82 #endif
83 
84 // no usleep() support
85 int usleep(unsigned int usec)
86 {
87     div_t dt = div(usec, 1000000);
88     struct timespec ts = { dt.quot, dt.rem * 1000 };
89 
90     return nanosleep(&ts, 0);
91 }
92 
93 
94 // gettimeofday() is declared, but is missing from the library
95 // It IS however defined in the Curtis-Wright X11 libraries, so
96 // we have to make the symbol 'weak'
97 #if defined(Q_CC_DIAB) && !defined(VXWORKS_DKM) && !defined(VXWORKS_RTP)
98 #  pragma weak gettimeofday
99 #endif
100 int gettimeofday(struct timeval *tv, void /*struct timezone*/ *)
101 {
102     // the compiler will optimize this and will only use one code path
103     if (sizeof(struct timeval) == sizeof(struct timespec)) {
104         int res = clock_gettime(CLOCK_REALTIME, (struct timespec *) tv);
105         if (!res)
106             tv->tv_usec /= 1000;
107         return res;
108     } else {
109         struct timespec ts;
110 
111         int res = clock_gettime(CLOCK_REALTIME, &ts);
112         if (!res) {
113             tv->tv_sec = ts.tv_sec;
114             tv->tv_usec = ts.tv_nsec / 1000;
115         }
116         return res;
117     }
118 }
119 
120 // neither getpagesize() or sysconf(_SC_PAGESIZE) are available
121 int getpagesize()
122 {
123 #if defined(_WRS_KERNEL)
124     return vmPageSizeGet();
125 #else
126     return sysconf(_SC_PAGESIZE);
127 #endif
128 }
129 
130 // symlinks are not supported (lstat is now just a call to stat - see qplatformdefs.h)
131 int symlink(const char *, const char *)
132 {
133     errno = EIO;
134     return -1;
135 }
136 
137 ssize_t readlink(const char *, char *, size_t)
138 {
139     errno = EIO;
140     return -1;
141 }
142 
143 // there's no truncate(), but ftruncate() support...
144 int truncate(const char *path, off_t length)
145 {
146     int fd = open(path, O_WRONLY, 00777);
147     if (fd >= 0) {
148         int res = ftruncate(fd, length);
149         int en = errno;
150         close(fd);
151         errno = en;
152         return res;
153     }
154     // errno is already set by open
155     return -1;
156 }
157 
158 
159 
160 // VxWorks doesn't know about passwd & friends.
161 // in order to avoid patching the unix fs path everywhere
162 // we introduce some dummy functions that simulate a single
163 // 'root' user on the system.
164 
165 uid_t getuid()
166 {
167     return 0;
168 }
169 
170 gid_t getgid()
171 {
172     return 0;
173 }
174 
175 uid_t geteuid()
176 {
177     return 0;
178 }
179 
180 struct passwd *getpwuid(uid_t uid)
181 {
182     static struct passwd pwbuf = { "root", 0, 0, 0, 0, 0, 0 };
183 
184     if (uid == 0) {
185         return &pwbuf;
186     } else {
187         errno = ENOENT;
188         return 0;
189     }
190 }
191 
192 struct group *getgrgid(gid_t gid)
193 {
194     static struct group grbuf = { "root", 0, 0, 0 };
195 
196     if (gid == 0) {
197         return &grbuf;
198     } else {
199         errno = ENOENT;
200         return 0;
201     }
202 }
203 
204 #ifdef __cplusplus
205 } // extern "C"
206 #endif
207 
208 #endif // Q_OS_VXWORKS
209