source: branches/samba-3.0/source/lib/os2ea.c@ 236

Last change on this file since 236 was 162, checked in by Paul Smedley, 17 years ago

Do not use hrtimer for random data - causes login delays

File size: 12.8 KB
Line 
1/* This file contains helper functions for OS/2 - don't try and compile on other platforms */
2
3#ifdef __OS2__
4
5#define INCL_LONGLONG
6#define INCL_DOS
7#define INCL_DOSPROCESS
8#define INCL_DOSPROFILE
9#define INCL_DOSMISC
10#define INCL_DOSMODULEMGR
11#define INCL_DOSERRORS
12//_SMB_H
13#include <os2.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <errno.h>
17#include <types.h>
18
19//YD for tmalloc
20#include <malloc.h>
21
22/* these define the attribute byte as seen by DOS */
23#define aRONLY (1L<<0) /* 0x01 */
24#define aHIDDEN (1L<<1) /* 0x02 */
25#define aSYSTEM (1L<<2) /* 0x04 */
26#define aVOLID (1L<<3) /* 0x08 */
27#define aDIR (1L<<4) /* 0x10 */
28#define aARCH (1L<<5) /* 0x20 */
29
30#ifndef TESTING
31#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
32#define CMD_KI_RDCNT (0x63)
33
34#include "local.h"
35#include "xfile.h"
36#include "pstring.h"
37#include "debug.h"
38#else
39#define DEBUG(a,b) (0)
40#endif
41
42#include <string.h>
43
44#ifndef ENOATTR
45#define ENOATTR 22
46#endif
47
48int os2_ftruncate(int fd, off_t size)
49{
50 // We call there __libc_Back_ioFileSizeSet directly instead of
51 // ftruncate to force it not to zero expanding files to optimize
52 // samba performance when copying files
53 int rc = __libc_Back_ioFileSizeSet(fd, size, 0);
54 if (rc < 0)
55 {
56 errno = -rc;
57 return -1;
58 }
59 return 0;
60}
61
62int os2_isattribute(char *path, unsigned short attr)
63{
64 HDIR hdirFindHandle = HDIR_CREATE;
65 FILEFINDBUF3 FindBuffer = {0}; /* Returned from FindFirst/Next */
66 USHORT dosattr; /* attribute to search for */
67 ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
68 ULONG ulFindCount = 1; /* Look for 1 file at a time */
69 APIRET rc = NO_ERROR; /* Return code */
70 if (attr==aARCH)
71 dosattr=MUST_HAVE_ARCHIVED;
72 else if (attr==aRONLY)
73 dosattr=MUST_HAVE_READONLY;
74 else if (attr==aSYSTEM)
75 dosattr=MUST_HAVE_SYSTEM;
76 else if (attr==aHIDDEN)
77 dosattr=MUST_HAVE_HIDDEN;
78
79 rc = DosFindFirst( path, /* File pattern - all files */
80 &hdirFindHandle, /* Directory search handle */
81 dosattr,
82 &FindBuffer, /* Result buffer */
83 ulResultBufLen, /* Result buffer length */
84 &ulFindCount, /* Number of entries to find */
85 FIL_STANDARD); /* Return Level 1 file info */
86
87 if (rc != NO_ERROR) {
88 rc = DosFindClose(hdirFindHandle); /* Close our directory handle */
89 return 1;
90
91 } else {
92 rc = DosFindClose(hdirFindHandle); /* Close our directory handle */
93 return 0;
94
95 } /* endif */
96
97}
98
99/* Functions below are based on APR random code */
100/* Licensed to the Apache Software Foundation (ASF) under one or more
101 * contributor license agreements. See the NOTICE file distributed with
102 * this work for additional information regarding copyright ownership.
103 * The ASF licenses this file to You under the Apache License, Version 2.0
104 * (the "License"); you may not use this file except in compliance with
105 * the License. You may obtain a copy of the License at
106 *
107 * http://www.apache.org/licenses/LICENSE-2.0
108 *
109 * Unless required by applicable law or agreed to in writing, software
110 * distributed under the License is distributed on an "AS IS" BASIS,
111 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
112 * See the License for the specific language governing permissions and
113 * limitations under the License.
114 */
115
116void os2_randget(char * buffer, int length)
117{
118 UCHAR randbyte();
119 unsigned int idx;
120
121 for (idx=0; idx<length; idx++)
122 buffer[idx] = randbyte();
123
124}
125
126/* A bunch of system information like memory & process stats.
127 * Not highly random but every bit helps....
128 */
129static UCHAR randbyte_sysinfo()
130{
131 UCHAR byte = 0;
132 UCHAR SysVars[100];
133 int b;
134
135 DosQuerySysInfo(1, QSV_FOREGROUND_PROCESS, SysVars, sizeof(SysVars));
136
137 for (b = 0; b < 100; b++) {
138 byte ^= SysVars[b];
139 }
140
141 return byte;
142}
143
144
145
146/* Similar in concept to randbyte_hrtimer() but accesses the CPU's internal
147 * counters which run at the CPU's MHz speed. We get separate
148 * idle / busy / interrupt cycle counts which should provide very good
149 * randomness due to interference of hardware events.
150 * This only works on newer CPUs (at least PPro or K6) and newer OS/2 versions
151 * which is why it's run-time linked.
152 */
153
154static HMODULE hDoscalls = 0;
155#define CMD_KI_RDCNT (0x63)
156
157typedef struct _CPUUTIL {
158 ULONG ulTimeLow; /* Low 32 bits of time stamp */
159 ULONG ulTimeHigh; /* High 32 bits of time stamp */
160 ULONG ulIdleLow; /* Low 32 bits of idle time */
161 ULONG ulIdleHigh; /* High 32 bits of idle time */
162 ULONG ulBusyLow; /* Low 32 bits of busy time */
163 ULONG ulBusyHigh; /* High 32 bits of busy time */
164 ULONG ulIntrLow; /* Low 32 bits of interrupt time */
165 ULONG ulIntrHigh; /* High 32 bits of interrupt time */
166} CPUUTIL;
167
168
169static UCHAR randbyte_perf()
170{
171 UCHAR byte = 0;
172 CPUUTIL util;
173 int c;
174
175#ifndef __INNOTEK_LIBC__
176 if (hDoscalls == 0) {
177 char failed_module[20];
178 ULONG rc;
179
180 rc = DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS",
181 &hDoscalls);
182
183 if (rc == 0) {
184 rc = DosQueryProcAddr(hDoscalls, 976, NULL, (PFN *)&DosPerfSysCall);
185
186 if (rc) {
187 DosPerfSysCall = NULL;
188 }
189 }
190 }
191
192 if (DosPerfSysCall) {
193 if (DosPerfSysCall(CMD_KI_RDCNT, (ULONG)&util, 0, 0) == 0) {
194 for (c = 0; c < sizeof(util); c++) {
195 byte ^= ((UCHAR *)&util)[c];
196 }
197 }
198 else {
199 DosPerfSysCall = NULL;
200 }
201 }
202#endif
203 return byte;
204}
205
206
207
208UCHAR randbyte()
209{
210 return randbyte_sysinfo() ^ randbyte_perf();
211}
212
213
214void maperrno(int rc)
215{
216 switch (rc)
217 {
218 case ERROR_PATH_NOT_FOUND :
219 case ERROR_FILE_NOT_FOUND : errno = ENOENT; break;
220 case ERROR_INVALID_HANDLE : errno = EBADF; break;
221 case ERROR_ACCESS_DENIED : errno = EACCES; break;
222 case ERROR_BUFFER_OVERFLOW :
223 case ERROR_NOT_ENOUGH_MEMORY : errno = ERANGE; break;
224 case ERROR_INVALID_EA_NAME : errno = ENOATTR; break;
225 case ERROR_INVALID_LEVEL :
226 case ERROR_INVALID_PARAMETER : errno = EINVAL; break;
227 case ERROR_SHARING_VIOLATION : errno = EACCES; break;
228 default : errno = EINVAL;
229 }
230}
231
232ssize_t unigetxattr (const char *path, int file, const char *name, void *value, size_t size)
233{
234 int rc, namelen;
235 EAOP2 eaop2 = {0};
236 PGEA2LIST pgea2list = NULL;
237 PFEA2LIST pfea2list = NULL;
238 char * p;
239
240 if ((!path && !file) || !name)
241 {
242 errno = EINVAL;
243 return -1;
244 }
245 namelen = strlen(name);
246 if (namelen > 0xFF)
247 {
248 errno = EINVAL;
249 return -1;
250 }
251 pgea2list = (PGEA2LIST)calloc(sizeof(GEA2LIST) + namelen + 1, 1);
252 pgea2list->list[0].oNextEntryOffset = 0;
253 pgea2list->list[0].cbName = namelen;
254 strcpy(pgea2list->list[0].szName, name);
255 pgea2list->cbList = sizeof(GEA2LIST) + namelen;
256
257 // max ea is 64kb
258 pfea2list = (PFEA2LIST)calloc(sizeof(FEA2LIST) + 0x10000, 1);
259 pfea2list->cbList = sizeof(FEA2LIST) + 0x10000;
260
261 eaop2.fpGEA2List = pgea2list;
262 eaop2.fpFEA2List = pfea2list;
263 eaop2.oError = 0;
264 do
265 {
266 if (path)
267 {
268 char npath[CCHMAXPATH + 1] = {0};
269 strncpy(npath, path, CCHMAXPATH);
270 for (p = npath; *p; p++)
271 {
272 if (*p == '/') *p = '\\';
273 }
274 rc = DosQueryPathInfo(npath, FIL_QUERYEASFROMLIST, &eaop2, sizeof(eaop2));
275 }
276 else
277 {
278 rc = DosQueryFileInfo( file, FIL_QUERYEASFROMLIST, &eaop2, sizeof(eaop2));
279 }
280 if (rc)
281 {
282 maperrno(rc);
283 rc = -1;
284 break;
285 }
286 if (strnicmp(pfea2list->list[0].szName, name, namelen) || pfea2list->list[0].cbValue == 0)
287 {
288 errno = ENOATTR;
289 rc = -1;
290 break;
291 }
292 rc = pfea2list->list[0].cbValue;
293 if (value)
294 {
295 if (size < rc)
296 {
297 errno = ERANGE;
298 rc = -1;
299 }
300 else
301 {
302 p = pfea2list->list[0].szName + pfea2list->list[0].cbName + 1;
303 memcpy(value, p, rc);
304 }
305 }
306 } while (0);
307 if (pgea2list)
308 {
309 free(pgea2list);
310 }
311 if (pgea2list)
312 {
313 free(pfea2list);
314 }
315 DEBUG(4,("unigetxattr : (%s:%d) %s %d\n", path ? path : "null", file, name, rc));
316 return rc;
317}
318
319ssize_t unilistxattr (const char *path, int file, char *list, size_t size)
320{
321 ssize_t gotsize = 0;
322 unsigned long ulCount = -1;
323 int rc;
324 char * buf, *p = list;
325 PFEA2 pfea;
326 FILESTATUS4 stat = {0};
327 char npath[CCHMAXPATH + 1] = {0};
328 if (!path && !file)
329 {
330 errno = EINVAL;
331 return -1;
332 }
333 if (path)
334 {
335 char * p;
336 strncpy(npath, path, CCHMAXPATH);
337 for (p = npath; *p; p++)
338 {
339 if (*p == '/') *p = '\\';
340 }
341 rc = DosQueryPathInfo(npath, FIL_QUERYEASIZE, &stat, sizeof(stat));
342 }
343 else
344 {
345 rc = DosQueryFileInfo( file, FIL_QUERYEASIZE, &stat, sizeof(stat));
346 }
347 if (rc)
348 {
349 DEBUG(4,("unilistxattr1 : (%s:%d) %d\n", path ? path : "null", file, rc));
350 maperrno(rc);
351 return -1;
352 }
353 if (stat.cbList <= 4)
354 {
355 // NO ea
356 return 0;
357 }
358 //YD DosEnumAttribute doesn't like high-mem buffers, get a low one.
359 buf = (char *)_tmalloc(stat.cbList * 2);
360 rc = DosEnumAttribute(path ? 1 : 0, path ? (PVOID)path : (PVOID)&file, 1, (PVOID)buf, stat.cbList * 2, &ulCount, 1);
361 if (rc)
362 {
363 DEBUG(4,("unilistxattr2 : (%s:%d) %d\n", path ? path : "null", file, rc));
364 maperrno(rc);
365 _tfree(buf);
366 return -1;
367 }
368 if (ulCount > 0)
369 for (pfea = (PFEA2)buf;;pfea = (PFEA2)((char *)pfea + pfea->oNextEntryOffset))
370 {
371 if (pfea->cbName > 0)
372 {
373 gotsize += pfea->cbName + 1;
374 if (p && size >= gotsize)
375 {
376 pfea->szName[pfea->cbName] = 0;
377 strcpy(p, pfea->szName);
378 p += strlen(p) + 1;
379 }
380 }
381 if (!pfea->oNextEntryOffset)
382 {
383 break;
384 }
385 }
386 _tfree(buf);
387 DEBUG(4,("unilistxattr : (%s:%d) %d\n", path ? path : "null", file, gotsize));
388 if (gotsize > size)
389 {
390 errno = ERANGE;
391 return list ? -1 : gotsize;
392 }
393 return gotsize;
394}
395
396int uniremovexattr (const char *path, int file, const char *name)
397{
398 int rc, namelen;
399 EAOP2 eaop2 = {0};
400 PFEA2LIST pfea2list = NULL;
401 char buf[300] = {0};
402
403 if ((!path && !file) || !name)
404 {
405 errno = EINVAL;
406 return -1;
407 }
408
409 namelen = strlen(name);
410 if (namelen > 0xFF)
411 {
412 errno = EINVAL;
413 return -1;
414 }
415
416 pfea2list = (PFEA2LIST)buf;
417 pfea2list->list[0].cbName = namelen;
418 pfea2list->list[0].cbValue = 0;
419 pfea2list->list[0].fEA = 0;
420 strcpy(pfea2list->list[0].szName, name);
421 pfea2list->cbList = sizeof(FEA2LIST) + namelen;
422 eaop2.fpFEA2List = pfea2list;
423
424 if (path)
425 {
426 char npath[CCHMAXPATH + 1] = {0};
427 char * p;
428 strncpy(npath, path, CCHMAXPATH);
429 for (p = npath; *p; p++)
430 {
431 if (*p == '/') *p = '\\';
432 }
433 rc = DosSetPathInfo(npath, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2), DSPI_WRTTHRU);
434 }
435 else
436 {
437 rc = DosSetFileInfo( file, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2));
438 }
439 if (rc)
440 {
441 maperrno(rc);
442 return -1;
443 }
444 return 0;
445}
446
447#ifndef XATTR_CREATE
448#define XATTR_CREATE 1
449#endif
450#ifndef XATTR_REPLACE
451#define XATTR_REPLACE 2
452#endif
453
454int unisetxattr (const char *path, int file, const char *name, const void *value, size_t size, int flags)
455{
456 int rc, namelen, totalsize;
457 EAOP2 eaop2 = {0};
458 PFEA2LIST pfea2list = NULL;
459 char * p;
460
461 if ((!path && !file) || !name || (!value && size))
462 {
463 errno = EINVAL;
464 return -1;
465 }
466 namelen = strlen(name);
467 if (namelen > 0xFF)
468 {
469 errno = EINVAL;
470 return -1;
471 }
472
473 if (flags & (XATTR_CREATE | XATTR_REPLACE))
474 {
475 ssize_t esize = unigetxattr(path, file, name, 0, 0);
476 if (flags & XATTR_CREATE && esize > 0)
477 {
478 errno = EEXIST;
479 return -1;
480 }
481 if (flags & XATTR_REPLACE && esize < 0)
482 {
483 errno = ENOATTR;
484 return -1;
485 }
486 }
487
488 totalsize = sizeof(FEA2LIST) + size + namelen + 1;
489
490 pfea2list = (PFEA2LIST)calloc(totalsize, 1);
491 pfea2list->cbList = totalsize;
492 pfea2list->list[0].oNextEntryOffset = 0;
493 pfea2list->list[0].cbName = namelen;
494 pfea2list->list[0].cbValue = size;
495 strcpy(pfea2list->list[0].szName, name);
496 if (value)
497 {
498 memcpy(pfea2list->list[0].szName + namelen + 1, value, size);
499 }
500 eaop2.fpFEA2List = pfea2list;
501
502 if (path)
503 {
504 char npath[CCHMAXPATH + 1] = {0};
505 char * p;
506 strncpy(npath, path, CCHMAXPATH);
507 for (p = npath; *p; p++)
508 {
509 if (*p == '/') *p = '\\';
510 }
511 rc = DosSetPathInfo(npath, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2), DSPI_WRTTHRU);
512 }
513 else
514 {
515 rc = DosSetFileInfo( file, FIL_QUERYEASIZE, &eaop2, sizeof(eaop2));
516 }
517 free(pfea2list);
518 if (rc)
519 {
520 maperrno(rc);
521 return -1;
522 }
523 return 0;
524}
525
526#endif
Note: See TracBrowser for help on using the repository browser.