]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/compat/posix/v2_0/src/misc.cxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / compat / posix / v2_0 / src / misc.cxx
1 //==========================================================================
2 //
3 //      misc.cxx
4 //
5 //      POSIX misc function implementations
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //==========================================================================
38 //#####DESCRIPTIONBEGIN####
39 //
40 // Author(s):           nickg
41 // Contributors:        nickg
42 // Date:                2000-07-18
43 // Purpose:             POSIX misc function implementation
44 // Description:         This file contains the implementation of miscellaneous POSIX
45 //                      functions that do not belong elsewhere.
46 //              
47 //              
48 //
49 //####DESCRIPTIONEND####
50 //
51 //==========================================================================
52
53 #include <pkgconf/system.h>
54 #include <pkgconf/hal.h>
55 #include <pkgconf/kernel.h>
56 #include <pkgconf/posix.h>
57
58 #include <cyg/kernel/ktypes.h>         // base kernel types
59 #include <cyg/infra/cyg_trac.h>        // tracing macros
60 #include <cyg/infra/cyg_ass.h>         // assertion macros
61
62 #include "pprivate.h"                   // POSIX private header
63
64 #include <unistd.h>
65 #ifdef CYGPKG_POSIX_UTSNAME
66 #include <sys/utsname.h>                // My header
67 #endif
68 #include <string.h>                     // strcpy
69 #include <limits.h>
70 #include <time.h>
71
72 #include <cyg/kernel/sched.hxx>
73
74 #include <cyg/kernel/sched.inl>
75
76 // -------------------------------------------------------------------------
77 // Supply some suitable values for constants that may not be present
78 // in all configurations.
79
80 #ifndef MQ_OPEN_MAX
81 #define MQ_OPEN_MAX 0
82 #endif
83 #ifndef MQ_PRIO_MAX
84 #define MQ_PRIO_MAX 0
85 #endif
86
87 // -------------------------------------------------------------------------
88
89 #define __string(_x) #_x
90 #define __xstring(_x) __string(_x)
91
92 // -------------------------------------------------------------------------
93 // uname()
94
95 #ifdef CYGPKG_POSIX_UTSNAME
96 __externC int uname( struct utsname *name )
97 {
98     CYG_REPORT_FUNCTYPE( "returning %d" );
99
100     strcpy( name->sysname, "eCos" );
101     strcpy( name->nodename, "" );       // should use gethostname()
102     strcpy( name->release, __xstring( CYGNUM_KERNEL_VERSION_MAJOR ) );
103     strcpy( name->version, __xstring( CYGNUM_KERNEL_VERSION_MINOR ) );
104     strcpy( name->machine, "" );
105
106     CYG_REPORT_RETVAL(0);
107     return 0;
108 }
109 #endif
110
111 // -------------------------------------------------------------------------
112 // sysconf()
113
114 #define SC_CASE( _name, _val ) case _name: return _val
115
116 __externC long sysconf( int name )
117 {
118
119     switch( name )
120     {
121         SC_CASE( _SC_AIO_LISTIO_MAX,                    AIO_LISTIO_MAX );
122         SC_CASE( _SC_AIO_MAX,                           AIO_MAX );
123         SC_CASE( _SC_AIO_PRIO_DELTA_MAX,                AIO_PRIO_DELTA_MAX );
124         SC_CASE( _SC_ARG_MAX,                           ARG_MAX );
125         SC_CASE( _SC_CHILD_MAX,                         CHILD_MAX );
126         SC_CASE( _SC_DELAYTIMER_MAX,                    DELAYTIMER_MAX );
127         SC_CASE( _SC_GETGR_R_SIZE_MAX,                  0 );
128         SC_CASE( _SC_GETPW_R_SIZE_MAX,                  0 );
129         SC_CASE( _SC_LOGIN_NAME_MAX,                    LOGIN_NAME_MAX );
130 #ifdef CYGPKG_POSIX_MQUEUES        
131         SC_CASE( _SC_MQ_OPEN_MAX,                       MQ_OPEN_MAX );
132         SC_CASE( _SC_MQ_PRIO_MAX,                       MQ_PRIO_MAX );
133 #endif
134         SC_CASE( _SC_NGROUPS_MAX,                       NGROUPS_MAX );
135         SC_CASE( _SC_OPEN_MAX,                          OPEN_MAX );
136         SC_CASE( _SC_PAGESIZE,                          PAGESIZE );
137         SC_CASE( _SC_RTSIG_MAX,                         RTSIG_MAX );
138 #ifdef CYGPKG_POSIX_SEMAPHORES
139         SC_CASE( _SC_SEM_NSEMS_MAX,                     SEM_NSEMS_MAX );
140         SC_CASE( _SC_SEM_VALUE_MAX,                     SEM_VALUE_MAX );
141 #endif
142         SC_CASE( _SC_SIGQUEUE_MAX,                      SIGQUEUE_MAX );
143         SC_CASE( _SC_STREAM_MAX,                        STREAM_MAX );
144 #ifdef CYGPKG_POSIX_PTHREAD
145         SC_CASE( _SC_THREAD_DESTRUCTOR_ITERATIONS,      PTHREAD_DESTRUCTOR_ITERATIONS );
146         SC_CASE( _SC_THREAD_KEYS_MAX,                   PTHREAD_KEYS_MAX );
147         SC_CASE( _SC_THREAD_STACK_MIN,                  PTHREAD_STACK_MIN );
148         SC_CASE( _SC_THREAD_THREADS_MAX,                PTHREAD_THREADS_MAX );
149 #endif
150         SC_CASE( _SC_TIMER_MAX,                         TIMER_MAX );
151         SC_CASE( _SC_TTY_NAME_MAX,                      TTY_NAME_MAX );
152         SC_CASE( _SC_TZNAME_MAX,                        TZNAME_MAX );
153         SC_CASE( _SC_VERSION,                           _POSIX_VERSION );
154
155 #ifdef CYGPKG_POSIX_TIMERS
156     case _SC_CLK_TCK:
157     {
158         struct timespec ts;
159         ts.tv_sec = 1;
160         ts.tv_nsec = 0;
161         cyg_tick_count ticks = cyg_timespec_to_ticks( &ts );
162         return ticks;
163     }
164 #endif
165
166     case _SC_ASYNCHRONOUS_IO:
167     #ifdef _POSIX_ASYNCHRONOUS_IO
168         return 1;
169     #else
170         return -1;
171     #endif
172             
173     case _SC_FSYNC:
174     #ifdef _POSIX_FSYNC
175         return 1;
176     #else
177         return -1;
178     #endif
179                 
180     case _SC_JOB_CONTROL:
181     #ifdef _POSIX_JOB_CONTROL
182         return 1;
183     #else
184         return -1;
185     #endif
186                     
187     case _SC_MAPPED_FILES:
188     #ifdef _POSIX_MAPPED_FILES
189         return 1;
190     #else
191         return -1;
192     #endif
193                         
194     case _SC_MEMLOCK:
195     #ifdef _POSIX_MEMLOCK
196         return 1;
197     #else
198         return -1;
199     #endif
200                             
201     case _SC_MEMLOCK_RANGE:
202     #ifdef _POSIX_MEMLOCK_RANGE
203         return 1;
204     #else
205         return -1        ;
206     #endif      
207                                 
208     case _SC_MEMORY_PROTECTION:
209     #ifdef _POSIX_MEMORY_PROTECTION
210         return 1;
211     #else
212         return -1;
213     #endif
214             
215     case _SC_MESSAGE_PASSING:
216     #ifdef _POSIX_MESSAGE_PASSING
217         return 1;
218     #else
219         return -1;
220     #endif
221                 
222     case _SC_PRIORITIZED_IO:
223     #ifdef _POSIX_PRIORITIZED_IO
224         return 1;
225     #else
226         return -1;
227     #endif
228                     
229     case _SC_PRIORITY_SCHEDULING:
230     #ifdef _POSIX_PRIORITY_SCHEDULING
231         return 1;
232     #else
233         return -1;
234     #endif
235                         
236     case _SC_REALTIME_SIGNALS:
237     #ifdef _POSIX_REALTIME_SIGNALS
238         return 1;
239     #else
240         return -1;
241     #endif
242                             
243     case _SC_SAVED_IDS:
244     #ifdef _POSIX_SAVED_IDS
245         return 1;
246     #else
247         return -1;
248     #endif
249                                 
250     case _SC_SEMAPHORES:
251     #ifdef _POSIX_SEMAPHORES
252         return 1;
253     #else
254         return -1;
255     #endif
256                                     
257     case _SC_SHARED_MEMORY_OBJECTS:
258     #ifdef _POSIX_SHARED_MEMORY_OBJECTS
259         return 1;
260     #else
261         return -1;
262     #endif
263                                         
264     case _SC_SYNCHRONIZED_IO:
265     #ifdef _POSIX_SYNCHRONIZED_IO
266         return 1;
267     #else
268         return -1;
269     #endif
270                                             
271     case _SC_THREADS:
272     #ifdef _POSIX_THREADS
273         return 1;
274     #else
275         return -1;
276     #endif
277                                                 
278     case _SC_THREAD_ATTR_STACKADDR:
279     #ifdef _POSIX_THREAD_ATTR_STACKADDR
280         return 1;
281     #else
282         return -1;
283     #endif
284                                                     
285     case _SC_THREAD_ATTR_STACKSIZE:
286     #ifdef _POSIX_THREAD_ATTR_STACKSIZE
287         return 1;
288     #else
289         return -1;
290     #endif
291                                                         
292     case _SC_THREAD_PRIO_INHERIT:
293     #ifdef _POSIX_THREAD_PRIO_INHERIT
294         return 1;
295     #else
296         return -1;
297     #endif
298                                                             
299     case _SC_THREAD_PRIO_PROTECT:
300     #ifdef _POSIX_THREAD_PRIO_PROTECT
301         return 1;
302     #else
303         return -1;
304     #endif
305                                                                 
306     case _SC_THREAD_PRIORITY_SCHEDULING:
307     #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
308         return 1;
309     #else
310         return -1;
311     #endif
312                                                                     
313     case _SC_THREAD_PROCESS_SHARED:
314     #ifdef _POSIX_THREAD_PROCESS_SHARED
315         return 1;
316     #else
317         return -1;
318     #endif
319                                                                         
320     case _SC_THREAD_SAFE_FUNCTIONS:
321     #ifdef _POSIX_THREAD_SAFE_FUNCTIONS
322         return 1;
323     #else
324         return -1;
325     #endif
326                                                                             
327     case _SC_TIMERS:
328     #ifdef _POSIX_TIMERS
329         return 1;
330     #else
331         return -1;
332     #endif
333                                                                                 
334
335     default:
336         errno = EINVAL;
337         return -1;
338     }
339 }
340
341 //==========================================================================
342 // Some trivial compatibility functions.
343 // These are merely present to permit existing code to be ported a little
344 // more easily, and to provide adequate standards compatibility.
345
346 __externC pid_t getpid    ( void ) { return 42; }
347 __externC pid_t getppid   ( void ) { return 41; }
348 __externC uid_t getuid    ( void ) { return 666; }
349 __externC uid_t geteuid   ( void ) { return 666; }
350 __externC gid_t getgid    ( void ) { return 88; }
351 __externC gid_t getegid   ( void ) { return 88; }
352 __externC int   setuid    ( uid_t uid ) { errno = EPERM; return -1; }
353 __externC int   setgid    ( uid_t gid ) { errno = EPERM; return -1; }
354 __externC int   getgroups ( int gidsetsize, gid_t grouplist[] ) { return 0; };
355 __externC pid_t getpgrp   ( void ) { return 42; }
356 __externC pid_t setsid    ( void ) { errno = EPERM; return -1; }
357 __externC int   setpgid   ( pid_t pid, pid_t pgid ) { errno = ENOSYS; return -1; }
358
359 //==========================================================================
360 // Exports to other packages
361
362 // -------------------------------------------------------------------------
363 // POSIX API function entry
364
365 __externC void cyg_posix_function_start()
366 {
367     Cyg_Thread *self = Cyg_Scheduler::get_current_thread();
368
369     // Inhibit ASR delivery in this function until it returns.
370     
371     self->set_asr_inhibit();
372 }
373
374 // -------------------------------------------------------------------------
375
376 __externC void cyg_posix_function_finish()
377 {
378     Cyg_Thread *self = Cyg_Scheduler::get_current_thread();
379
380     // Re-allow ASR delivery.
381     
382     self->clear_asr_inhibit();
383
384     // After clearing the inhibit flag, blip the scheduler lock
385     // to get any pending ASRs delivered.
386     Cyg_Scheduler::lock();
387     Cyg_Scheduler::unlock();
388 }
389
390 // -------------------------------------------------------------------------
391 // EOF misc.cxx