Skip to content

Commit 0f28841

Browse files
authored
Merge pull request #4364 from mysterywolf/unistd
[libc][unistd] add getxxid() functions
2 parents 88d3bb7 + 19c8cab commit 0f28841

File tree

6 files changed

+100
-30
lines changed

6 files changed

+100
-30
lines changed

components/libc/compilers/armlibc/sys/types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
typedef int32_t clockid_t;
1717
typedef int32_t key_t; /* Used for interprocess communication. */
18-
typedef int32_t pid_t; /* Used for process IDs and process group IDs. */
18+
typedef int pid_t; /* Used for process IDs and process group IDs. */
19+
typedef unsigned short uid_t;
20+
typedef unsigned short gid_t;
1921
#ifndef ARCH_CPU_64BIT
2022
typedef signed int ssize_t; /* Used for a count of bytes or an error indication. */
2123
#else

components/libc/compilers/armlibc/sys/unistd.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,11 @@ char * ttyname (int desc);
7373
unsigned int sleep(unsigned int seconds);
7474
int usleep(useconds_t usec);
7575

76+
pid_t getpid(void);
77+
pid_t getppid(void);
78+
uid_t getuid(void);
79+
uid_t geteuid(void);
80+
gid_t getgid(void);
81+
gid_t getegid(void);
82+
7683
#endif /* _SYS_UNISTD_H */

components/libc/compilers/common/unistd.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,40 @@ int usleep(useconds_t usec)
4949
return 0;
5050
}
5151
RTM_EXPORT(usleep);
52+
53+
pid_t getpid(void)
54+
{
55+
/*TODO*/
56+
return 0;
57+
}
58+
RTM_EXPORT(getpid);
59+
60+
pid_t getppid(void)
61+
{
62+
return 0;
63+
}
64+
RTM_EXPORT(getppid);
65+
66+
uid_t getuid(void)
67+
{
68+
return 0; /*ROOT*/
69+
}
70+
RTM_EXPORT(getuid);
71+
72+
uid_t geteuid(void)
73+
{
74+
return 0; /*ROOT*/
75+
}
76+
RTM_EXPORT(geteuid);
77+
78+
gid_t getgid(void)
79+
{
80+
return 0; /*ROOT*/
81+
}
82+
RTM_EXPORT(getgid);
83+
84+
gid_t getegid(void)
85+
{
86+
return 0; /*ROOT*/
87+
}
88+
RTM_EXPORT(getegid);

components/libc/compilers/dlib/sys/types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
typedef int32_t clockid_t;
1616
typedef int32_t key_t; /* Used for interprocess communication. */
17-
typedef int32_t pid_t; /* Used for process IDs and process group IDs. */
17+
typedef int pid_t; /* Used for process IDs and process group IDs. */
18+
typedef unsigned short uid_t;
19+
typedef unsigned short gid_t;
1820
#ifndef ARCH_CPU_64BIT
1921
typedef signed int ssize_t; /* Used for a count of bytes or an error indication. */
2022
#else

components/libc/compilers/dlib/sys/unistd.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@ char * ttyname (int desc);
4747
unsigned int sleep(unsigned int seconds);
4848
int usleep(useconds_t usec);
4949

50+
pid_t getpid(void);
51+
pid_t getppid(void);
52+
uid_t getuid(void);
53+
uid_t geteuid(void);
54+
gid_t getgid(void);
55+
gid_t getegid(void);
56+
5057
#endif /* _SYS_UNISTD_H */

components/libc/compilers/newlib/syscalls.c

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
* Date Author Notes
88
* 2021-02-11 Meco Man remove _gettimeofday_r() and _times_r()
99
* 2020-02-13 Meco Man re-implement exit() and abort()
10+
* 2020-02-21 Meco Man improve and beautify syscalls
1011
*/
1112

1213
#include <reent.h>
13-
#include <sys/errno.h>
14-
#include <sys/time.h>
14+
#include <errno.h>
1515
#include <stdio.h>
16+
#include <sys/time.h>
1617

1718
#include <rtthread.h>
1819

@@ -34,11 +35,19 @@ __errno ()
3435
}
3536
#endif
3637

38+
int
39+
_getpid_r(struct _reent *ptr)
40+
{
41+
return 0;
42+
}
43+
3744
int
3845
_close_r(struct _reent *ptr, int fd)
3946
{
4047
#ifndef RT_USING_DFS
41-
return 0;
48+
/* return "not supported" */
49+
ptr->_errno = ENOTSUP;
50+
return -1;
4251
#else
4352
return close(fd);
4453
#endif
@@ -76,20 +85,14 @@ _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
7685
return -1;
7786
}
7887

79-
int
80-
_getpid_r(struct _reent *ptr)
81-
{
82-
return 0;
83-
}
84-
8588
int
8689
_isatty_r(struct _reent *ptr, int fd)
8790
{
88-
if (fd >=0 && fd < 3) return 1;
91+
if (fd >=0 && fd < 3)
92+
return 1;
8993

90-
/* return "not supported" */
91-
ptr->_errno = ENOTSUP;
92-
return -1;
94+
ptr->_errno = ENOTTY ;
95+
return 0;
9396
}
9497

9598
int
@@ -112,7 +115,9 @@ _off_t
112115
_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
113116
{
114117
#ifndef RT_USING_DFS
115-
return 0;
118+
/* return "not supported" */
119+
ptr->_errno = ENOTSUP;
120+
return -1;
116121
#else
117122
_off_t rc;
118123

@@ -125,7 +130,9 @@ int
125130
_mkdir_r(struct _reent *ptr, const char *name, int mode)
126131
{
127132
#ifndef RT_USING_DFS
128-
return 0;
133+
/* return "not supported" */
134+
ptr->_errno = ENOTSUP;
135+
return -1;
129136
#else
130137
int rc;
131138

@@ -138,7 +145,9 @@ int
138145
_open_r(struct _reent *ptr, const char *file, int flags, int mode)
139146
{
140147
#ifndef RT_USING_DFS
141-
return 0;
148+
/* return "not supported" */
149+
ptr->_errno = ENOTSUP;
150+
return -1;
142151
#else
143152
int rc;
144153

@@ -151,7 +160,9 @@ _ssize_t
151160
_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
152161
{
153162
#ifndef RT_USING_DFS
154-
return 0;
163+
/* return "not supported" */
164+
ptr->_errno = ENOTSUP;
165+
return -1;
155166
#else
156167
_ssize_t rc;
157168

@@ -164,7 +175,9 @@ int
164175
_rename_r(struct _reent *ptr, const char *old, const char *new)
165176
{
166177
#ifndef RT_USING_DFS
167-
return 0;
178+
/* return "not supported" */
179+
ptr->_errno = ENOTSUP;
180+
return -1;
168181
#else
169182
int rc;
170183

@@ -184,7 +197,9 @@ int
184197
_stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
185198
{
186199
#ifndef RT_USING_DFS
187-
return 0;
200+
/* return "not supported" */
201+
ptr->_errno = ENOTSUP;
202+
return -1;
188203
#else
189204
int rc;
190205

@@ -197,6 +212,8 @@ int
197212
_unlink_r(struct _reent *ptr, const char *file)
198213
{
199214
#ifndef RT_USING_DFS
215+
/* return "not supported" */
216+
ptr->_errno = ENOTSUP;
200217
return -1;
201218
#else
202219
return unlink(file);
@@ -211,11 +228,11 @@ _wait_r(struct _reent *ptr, int *status)
211228
return -1;
212229
}
213230

214-
#ifdef RT_USING_DEVICE
215231
_ssize_t
216232
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
217233
{
218234
#ifndef RT_USING_DFS
235+
#ifdef RT_USING_DEVICE
219236
if (fileno(stdout) == fd)
220237
{
221238
rt_device_t console;
@@ -225,15 +242,18 @@ _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
225242
}
226243

227244
return 0;
228-
245+
#else
246+
/* return "not supported" */
247+
ptr->_errno = ENOTSUP;
248+
return -1;
249+
#endif /*RT_USING_DEVICE*/
229250
#else
230251
_ssize_t rc;
231252

232253
rc = write(fd, buf, nbytes);
233254
return rc;
234255
#endif
235256
}
236-
#endif
237257

238258
/* Memory routine */
239259
void *
@@ -304,11 +324,6 @@ void __libc_init_array(void)
304324
/* we not use __libc init_aray to initialize C++ objects */
305325
}
306326

307-
uid_t getuid(void)
308-
{
309-
return 0;
310-
}
311-
312327
mode_t umask(mode_t mask)
313328
{
314329
return 022;
@@ -320,7 +335,7 @@ int flock(int fd, int operation)
320335
}
321336

322337
/*
323-
These functions will be implemented and replaced by the 'common/time.c' file
338+
These functions are implemented and replaced by the 'common/time.c' file
324339
int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
325340
_CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
326341
*/

0 commit comments

Comments
 (0)