Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:39

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2012.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <tmacros.h>
0034 #include "test_support.h"
0035 
0036 #include <unistd.h>
0037 #include <errno.h>
0038 #include <limits.h>
0039 #include <sys/types.h>
0040 
0041 const char rtems_test_name[] = "PSXID 1";
0042 
0043 /* forward declarations to avoid warnings */
0044 rtems_task Init(rtems_task_argument argument);
0045 void test_gid(void);
0046 void test_uid(void);
0047 void test_pid(void);
0048 void test_getlogin(void);
0049 
0050 void test_gid(void)
0051 {
0052   gid_t gid;
0053   int sc;
0054 
0055   gid = getegid();
0056   rtems_test_assert( gid == 0 );
0057   printf( "getegid = %d\n", gid );
0058 
0059   gid = getgid();
0060   rtems_test_assert( gid == 0 );
0061   printf( "getgid = %d\n", gid );
0062 
0063   puts( "setgid(5)" );
0064   sc = setgid(5);
0065   rtems_test_assert( sc == 0 );
0066 
0067   gid = getegid();
0068   rtems_test_assert( gid == 0 );
0069   printf( "getegid = %d\n", gid );
0070 
0071   gid = getgid();
0072   rtems_test_assert( gid == 5 );
0073   printf( "getgid = %d\n", gid );
0074 
0075   puts( "setegid(5)" );
0076   sc = setegid(5);
0077   rtems_test_assert( sc == 0 );
0078 
0079   gid = getegid();
0080   rtems_test_assert( gid == 5 );
0081   printf( "getegid = %d\n", gid );
0082 
0083   gid = getgid();
0084   rtems_test_assert( gid == 5 );
0085   printf( "getgid = %d\n", gid );
0086 
0087   puts( "setgid(0)" );
0088   sc = setgid(0);
0089   rtems_test_assert( sc == 0 );
0090 
0091   puts( "setegid(0)" );
0092   sc = setegid(0);
0093   rtems_test_assert( sc == 0 );
0094 
0095   errno = 0;
0096   puts( "setpgid(getpid(), 10) - ENOSYS" );
0097   sc = setpgid( getpid(), 10 );
0098   rtems_test_assert( sc == -1 );
0099   rtems_test_assert( errno == ENOSYS );
0100 }
0101 
0102 void test_uid(void)
0103 {
0104   uid_t uid;
0105   int sc;
0106 
0107   uid = geteuid();
0108   rtems_test_assert( uid == 0 );
0109   printf( "geteuid = %d\n", uid );
0110 
0111   uid = getuid();
0112   rtems_test_assert( uid == 0 );
0113   printf( "getuid = %d\n", uid );
0114 
0115   puts( "setuid(5)" );
0116   sc = setuid(5);
0117   rtems_test_assert( sc == 0 );
0118 
0119   uid = geteuid();
0120   rtems_test_assert( uid == 0 );
0121   printf( "geteuid = %d\n", uid );
0122 
0123   uid = getuid();
0124   rtems_test_assert( uid == 5 );
0125   printf( "getuid = %d\n", uid );
0126 
0127   puts( "seteuid(5)" );
0128   sc = seteuid(5);
0129   rtems_test_assert( sc == 0 );
0130 
0131   uid = geteuid();
0132   rtems_test_assert( uid == 5 );
0133   printf( "geteuid = %d\n", uid );
0134 
0135   uid = getuid();
0136   rtems_test_assert( uid == 5 );
0137   printf( "getuid = %d\n", uid );
0138 
0139   puts( "seteuid(0)" );
0140   sc = seteuid(0);
0141   rtems_test_assert( sc == 0 );
0142 
0143   puts( "setuid(0)" );
0144   sc = setuid(0);
0145   rtems_test_assert( sc == 0 );
0146 }
0147 
0148 pid_t __getpid(void);
0149 int issetugid(void);
0150 
0151 void test_pid(void)
0152 {
0153   pid_t pid;
0154   int   sc;
0155 
0156   pid = getpid();
0157   printf( "getpid = %d\n", pid );
0158 
0159   pid = __getpid();
0160   printf( "__getpid = %d\n", pid );
0161 
0162   pid = getppid();
0163   printf( "getppid = %d\n", pid );
0164 
0165   puts( "setsid - EPERM" );
0166   pid = setsid();
0167   rtems_test_assert( pid == -1 );
0168   rtems_test_assert( errno == EPERM );
0169 
0170   sc = issetugid();
0171   rtems_test_assert( sc == 0 );
0172 
0173   puts( "getpgrp - return local node - OK" );
0174   pid = getpgrp();
0175   printf( "getpgrp returned %d\n", pid ); 
0176 }
0177 
0178 void test_getlogin(void)
0179 {
0180   int sc;
0181   char ch;
0182 
0183   puts( "setuid(5)" );
0184   sc = setuid(5);
0185   rtems_test_assert( sc == 0 );
0186   printf( "getlogin() -- (%s)\n", getlogin() );
0187 
0188   puts( "setuid(0)" );
0189   sc = setuid(0);
0190   rtems_test_assert( sc == 0 );
0191   printf( "getlogin() -- (%s)\n", getlogin() );
0192 
0193   puts( "getlogin_r(NULL, LOGIN_NAME_MAX) -- EFAULT" );
0194   sc = getlogin_r( NULL, LOGIN_NAME_MAX );
0195   rtems_test_assert( sc == EFAULT );
0196 
0197   puts( "getlogin_r(buffer, 0) -- ERANGE" );
0198   sc = getlogin_r( &ch, 0 );
0199   rtems_test_assert( sc == ERANGE );
0200 }
0201 
0202 rtems_task Init(
0203   rtems_task_argument argument
0204 )
0205 {
0206   TEST_BEGIN();
0207 
0208   test_gid();
0209   puts( "" );
0210 
0211   test_uid();
0212   puts( "" );
0213 
0214   test_pid();
0215   puts( "" );
0216 
0217   test_getlogin();
0218 
0219   TEST_END();
0220 
0221   rtems_test_exit(0);
0222 }
0223 
0224 /* configuration information */
0225 
0226 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0227 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0228 
0229 #define CONFIGURE_MAXIMUM_TASKS                  1
0230 /* so we can write /etc/passwd and /etc/group */
0231 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0232 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0233 
0234 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0235 
0236 #define CONFIGURE_INIT
0237 
0238 #include <rtems/confdefs.h>
0239 /* end of file */