1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qglobal.h"
43 
44 #ifdef Q_OS_VXWORKS
45 
46 #include "qplatformdefs.h"
47 #include "qfunctions_vxworks.h"
48 
49 #if defined(_WRS_KERNEL)
50 #include <vmLib.h>
51 #endif
52 #include <selectLib.h>
53 #include <ioLib.h>
54 
55 QT_USE_NAMESPACE
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 // no lfind() - used by the TIF image format
62 void *lfind(const void* key, const void* base, size_t* elements, size_t size,
63             int (*compare)(const void*, const void*))
64 {
65     const char* current = (char*) base;
66     const char* const end = (char*) (current + (*elements) * size);
67     while (current != end) {
68         if (compare(current, key) == 0)
69             return (void*)current;
70         current += size;
71     }
72     return 0;
73 }
74 
75 
76 // no rand_r(), but rand()
77 // NOTE: this implementation is wrong for multi threaded applications,
78 // but there is no way to get it right on VxWorks (in kernel mode)
79 int rand_r(unsigned int * /*seedp*/)
80 {
81     return rand();
82 }
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