mirror of
https://git.rtems.org/rtems-libbsd/
synced 2025-05-13 15:19:18 +08:00
Add socket fstat() support
This commit is contained in:
parent
609df33954
commit
bada2f77d0
@ -240,17 +240,26 @@ soo_poll(struct file *fp, int events, struct ucred *active_cred,
|
|||||||
#endif
|
#endif
|
||||||
return (sopoll(so, events, fp->f_cred, td));
|
return (sopoll(so, events, fp->f_cred, td));
|
||||||
}
|
}
|
||||||
|
#endif /* __rtems__ */
|
||||||
|
|
||||||
|
#ifndef __rtems__
|
||||||
int
|
int
|
||||||
soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
|
soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
|
||||||
struct thread *td)
|
struct thread *td)
|
||||||
{
|
{
|
||||||
struct socket *so = fp->f_data;
|
struct socket *so = fp->f_data;
|
||||||
|
#else /* __rtems__ */
|
||||||
|
static int
|
||||||
|
soo_stat(struct socket *so, struct stat *ub)
|
||||||
|
{
|
||||||
|
#endif /* __rtems__ */
|
||||||
#ifdef MAC
|
#ifdef MAC
|
||||||
int error;
|
int error;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef __rtems__
|
||||||
bzero((caddr_t)ub, sizeof (*ub));
|
bzero((caddr_t)ub, sizeof (*ub));
|
||||||
|
#endif /* __rtems__ */
|
||||||
ub->st_mode = S_IFSOCK;
|
ub->st_mode = S_IFSOCK;
|
||||||
#ifdef MAC
|
#ifdef MAC
|
||||||
error = mac_socket_check_stat(active_cred, so);
|
error = mac_socket_check_stat(active_cred, so);
|
||||||
@ -270,10 +279,27 @@ soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
|
|||||||
/* Unlocked read. */
|
/* Unlocked read. */
|
||||||
if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
|
if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
|
||||||
ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
|
ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
|
||||||
|
#ifndef __rtems__
|
||||||
ub->st_uid = so->so_cred->cr_uid;
|
ub->st_uid = so->so_cred->cr_uid;
|
||||||
ub->st_gid = so->so_cred->cr_gid;
|
ub->st_gid = so->so_cred->cr_gid;
|
||||||
|
#else /* __rtems__ */
|
||||||
|
ub->st_uid = BSD_DEFAULT_UID;
|
||||||
|
ub->st_gid = BSD_DEFAULT_GID;
|
||||||
|
#endif /* __rtems__ */
|
||||||
return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
|
return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
|
||||||
}
|
}
|
||||||
|
#ifdef __rtems__
|
||||||
|
static int
|
||||||
|
rtems_bsd_soo_stat(
|
||||||
|
const rtems_filesystem_location_info_t *loc,
|
||||||
|
struct stat *buf
|
||||||
|
)
|
||||||
|
{
|
||||||
|
struct socket *so = rtems_bsd_loc_to_f_data(loc);
|
||||||
|
int error = soo_stat(so, buf);
|
||||||
|
|
||||||
|
return rtems_bsd_error_to_status_and_errno(error);
|
||||||
|
}
|
||||||
#endif /* __rtems__ */
|
#endif /* __rtems__ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -321,7 +347,7 @@ const rtems_filesystem_file_handlers_r socketops = {
|
|||||||
.write_h = rtems_filesystem_default_write,
|
.write_h = rtems_filesystem_default_write,
|
||||||
.ioctl_h = rtems_filesystem_default_ioctl,
|
.ioctl_h = rtems_filesystem_default_ioctl,
|
||||||
.lseek_h = rtems_filesystem_default_lseek,
|
.lseek_h = rtems_filesystem_default_lseek,
|
||||||
.fstat_h = rtems_filesystem_default_fstat,
|
.fstat_h = rtems_bsd_soo_stat,
|
||||||
.ftruncate_h = rtems_filesystem_default_ftruncate,
|
.ftruncate_h = rtems_filesystem_default_ftruncate,
|
||||||
.fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
|
.fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
|
||||||
.fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
|
.fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
@ -46,6 +47,14 @@
|
|||||||
|
|
||||||
#define TEST_NAME "LIBBSD SYSCALLS 1"
|
#define TEST_NAME "LIBBSD SYSCALLS 1"
|
||||||
|
|
||||||
|
typedef void (*no_mem_test_body)(int fd);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
no_mem_test_body body;
|
||||||
|
int fd;
|
||||||
|
rtems_id master_task;
|
||||||
|
} no_mem_test;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int domain;
|
int domain;
|
||||||
int type;
|
int type;
|
||||||
@ -207,6 +216,62 @@ static socket_test socket_tests[] = {
|
|||||||
{ AF_IEEE80211, SOCK_DGRAM, 0, EPROTONOSUPPORT }
|
{ AF_IEEE80211, SOCK_DGRAM, 0, EPROTONOSUPPORT }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
no_mem_task(rtems_task_argument arg)
|
||||||
|
{
|
||||||
|
const no_mem_test *self = (const no_mem_test *) arg;
|
||||||
|
rtems_status_code sc;
|
||||||
|
void *greedy;
|
||||||
|
|
||||||
|
assert(rtems_configuration_get_unified_work_area());
|
||||||
|
|
||||||
|
greedy = rtems_workspace_greedy_allocate(NULL, 0);
|
||||||
|
(*self->body)(self->fd);
|
||||||
|
rtems_workspace_greedy_free(greedy);
|
||||||
|
|
||||||
|
sc = rtems_event_transient_send(self->master_task);
|
||||||
|
assert(sc == RTEMS_SUCCESSFUL);
|
||||||
|
|
||||||
|
sc = rtems_task_suspend(RTEMS_SELF);
|
||||||
|
assert(sc == RTEMS_SUCCESSFUL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
do_no_mem_test(no_mem_test_body body, int fd)
|
||||||
|
{
|
||||||
|
no_mem_test test = {
|
||||||
|
.body = body,
|
||||||
|
.fd = fd,
|
||||||
|
.master_task = rtems_task_self()
|
||||||
|
};
|
||||||
|
rtems_status_code sc;
|
||||||
|
rtems_id id;
|
||||||
|
rtems_resource_snapshot snapshot;
|
||||||
|
|
||||||
|
rtems_resource_snapshot_take(&snapshot);
|
||||||
|
|
||||||
|
sc = rtems_task_create(
|
||||||
|
rtems_build_name('N', 'M', 'E', 'M'),
|
||||||
|
RTEMS_MINIMUM_PRIORITY,
|
||||||
|
RTEMS_MINIMUM_STACK_SIZE,
|
||||||
|
RTEMS_DEFAULT_MODES,
|
||||||
|
RTEMS_DEFAULT_ATTRIBUTES,
|
||||||
|
&id
|
||||||
|
);
|
||||||
|
assert(sc == RTEMS_SUCCESSFUL);
|
||||||
|
|
||||||
|
sc = rtems_task_start(id, no_mem_task, (rtems_task_argument) &test);
|
||||||
|
assert(sc == RTEMS_SUCCESSFUL);
|
||||||
|
|
||||||
|
sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
|
||||||
|
assert(sc == RTEMS_SUCCESSFUL);
|
||||||
|
|
||||||
|
sc = rtems_task_delete(id);
|
||||||
|
assert(sc == RTEMS_SUCCESSFUL);
|
||||||
|
|
||||||
|
assert(rtems_resource_snapshot_check(&snapshot));
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_socket(const socket_test *st)
|
test_socket(const socket_test *st)
|
||||||
{
|
{
|
||||||
@ -294,6 +359,44 @@ test_socket_unsupported_ops(void)
|
|||||||
assert(rtems_resource_snapshot_check(&snapshot));
|
assert(rtems_resource_snapshot_check(&snapshot));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
no_mem_socket_fstat(int fd)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
int rv;
|
||||||
|
|
||||||
|
rv = fstat(fd, &st);
|
||||||
|
assert(rv == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_socket_fstat(void)
|
||||||
|
{
|
||||||
|
mode_t canrecv = S_IRUSR | S_IRGRP | S_IROTH;
|
||||||
|
mode_t cansend = S_IWUSR | S_IWGRP | S_IWOTH;
|
||||||
|
rtems_resource_snapshot snapshot;
|
||||||
|
struct stat st;
|
||||||
|
int sd;
|
||||||
|
int rv;
|
||||||
|
|
||||||
|
puts("test socket fstat");
|
||||||
|
|
||||||
|
rtems_resource_snapshot_take(&snapshot);
|
||||||
|
|
||||||
|
sd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
|
assert(sd >= 0);
|
||||||
|
|
||||||
|
do_no_mem_test(no_mem_socket_fstat, sd);
|
||||||
|
|
||||||
|
rv = fstat(sd, &st);
|
||||||
|
assert(rv == 0);
|
||||||
|
assert(st.st_mode == (S_IFSOCK | canrecv | cansend));
|
||||||
|
|
||||||
|
rv = close(sd);
|
||||||
|
assert(rv == 0);
|
||||||
|
|
||||||
|
assert(rtems_resource_snapshot_check(&snapshot));
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_main(void)
|
test_main(void)
|
||||||
@ -302,6 +405,7 @@ test_main(void)
|
|||||||
test_sockets();
|
test_sockets();
|
||||||
|
|
||||||
test_socket_unsupported_ops();
|
test_socket_unsupported_ops();
|
||||||
|
test_socket_fstat();
|
||||||
|
|
||||||
puts("*** END OF " TEST_NAME " TEST ***");
|
puts("*** END OF " TEST_NAME " TEST ***");
|
||||||
exit(0);
|
exit(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user