diff options
Diffstat (limited to 'test/misc')
| -rw-r--r-- | test/misc/Makefile.in | 7 | ||||
| -rw-r--r-- | test/misc/tst-hasmntopt.c | 47 | ||||
| -rwxr-xr-x | test/misc/tst-msgctl.c | 126 | ||||
| -rw-r--r-- | test/misc/tst-rlimit.c | 157 | ||||
| -rw-r--r-- | test/misc/tst-rlimit64.c | 1 | ||||
| -rw-r--r-- | test/misc/tst-semctl.c | 97 | ||||
| -rw-r--r-- | test/misc/tst-shmctl.c | 84 |
7 files changed, 519 insertions, 0 deletions
diff --git a/test/misc/Makefile.in b/test/misc/Makefile.in index a97b124..31bb7e1 100644 --- a/test/misc/Makefile.in +++ b/test/misc/Makefile.in @@ -6,8 +6,15 @@ TESTS_DISABLED := outb tst-fnmatch bug-glob1 tst-gnuglob ifeq ($(TARGET_ARCH),avr32) TESTS_DISABLED += tst-inotify endif +ifeq ($(TARGET_ARCH),bfin) +TESTS_DISABLED += tst-rlimit tst-rlimit64 +endif +ifeq ($(TARGET_ARCH),m68k) +TESTS_DISABLED += tst-rlimit tst-rlimit64 +endif CFLAGS_dirent64 := -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 +CFLAGS_tst-rlimit64 := -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 DODIFF_dirent := 1 DODIFF_dirent64 := 1 diff --git a/test/misc/tst-hasmntopt.c b/test/misc/tst-hasmntopt.c new file mode 100644 index 0000000..17655bd --- /dev/null +++ b/test/misc/tst-hasmntopt.c @@ -0,0 +1,47 @@ +/* Copyright (C) 2020 by Yann Sionneau <yann@sionneau.net> */ + +#include <stdio.h> +#include <mntent.h> +#include <stdlib.h> +#include <string.h> + +static int +do_test (void) +{ + char *res; + struct mntent m; + + /* check that "ro" does not match "erROr" */ + m.mnt_opts = "error"; + res = hasmntopt (&m, MNTOPT_RO); + if (res != NULL) { + puts ("error: hasmntopt() picked up non existing option"); + exit (1); + } + + /* check that "ro" does not match "remount-ro" */ + m.mnt_opts = "rw,relatime,errors=remount-ro"; + res = hasmntopt (&m, MNTOPT_RO); + if (res != NULL) { + puts ("error: hasmntopt() picked up non existing option"); + exit (1); + } + + /* check that "ro" does match "ro" */ + m.mnt_opts = "noatime,ro"; + res = hasmntopt (&m, MNTOPT_RO); + if (res == NULL) { + puts ("error: hasmntopt() did not pick up an existing option"); + exit (1); + } + + if (strncmp(res, "ro", 2) != 0) { + puts ("error: hasmntopt() did not return a pointer to corresponding option"); + exit (1); + } + + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/test/misc/tst-msgctl.c b/test/misc/tst-msgctl.c new file mode 100755 index 0000000..ac6ae34 --- /dev/null +++ b/test/misc/tst-msgctl.c @@ -0,0 +1,126 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ipc.h> +#include <sys/msg.h> +#include <sys/sem.h> +#include <sys/shm.h> +#include <sys/types.h> +#include <time.h> +#include <unistd.h> + +// Define the message structure +struct message { + long mtype; // Message Type + char mtext[100]; // Message body +}; + +struct timespec ts = { + .tv_sec = 3468960000, // 2075-12-05 Destination timestamp + .tv_nsec = 0 +}; + +void print_msqid_ds(struct msqid_ds *buf) { + printf("perms: %o\n", buf->msg_perm.mode); + printf("UID: %d\n", buf->msg_perm.uid); + printf("GID: %d\n", buf->msg_perm.gid); + printf("Current number of bytes in the queue: %d\n", buf->msg_cbytes); + printf("Number of messages in the queue: %d\n", buf->msg_qnum); + printf("Maximum number of bytes allowed in the queue: %d\n", buf->msg_qbytes); + printf("Last sent time: %s", buf->msg_stime ? ctime(&buf->msg_stime) + 4 : "Not set \n"); + printf("Last received time: %s", buf->msg_rtime ? ctime(&buf->msg_rtime) + 4 : "Not set \n"); +} + + +int main() { + + struct timespec ts_init, ts_final; + + // Save system time + if (clock_gettime(CLOCK_REALTIME, &ts_init) == -1) { + perror("Error getting time"); + return 1; + } + + if (clock_settime(CLOCK_REALTIME, &ts) == -1) { // Set the time to after 2038 + perror("Error setting time"); + return 1; + } + + key_t key = ftok(".", 123); + if (key == -1) { + perror("ftok"); + return 1; + } + + int msqid = msgget(key, 0644 | IPC_CREAT); // Set to write/read only (not full permissions) + if (msqid == -1) { + perror("msgget"); + return 1; + } + + // Get message queue status + struct msqid_ds buf; + memset(&buf, 0, sizeof(buf)); // Clear the structure + if (msgctl(msqid, IPC_STAT, &buf) == -1) { + perror("msgctl"); + return 1; + } + + // Print message queue information + printf("=== Initial queue status ===\n"); + printf("key: %x\n", key); + printf("msqid: %d\n", msqid); + print_msqid_ds(&buf); + + // Prepare the message to be sent + struct message msg = {0}; + msg.mtype = 1; // Set the message type to 1 + int i =0; + + for(i =0; i< 2; i++) + { + snprintf(msg.mtext, sizeof(msg.mtext), "Hello, Message Queue %d!", i); + msg.mtext[sizeof(msg.mtext) - 1] = '\0'; // Ensure the message ends with a '\0' + + // Send the message + if (msgsnd(msqid, &msg, strlen(msg.mtext) + 1, 0) == -1) { + perror("msgsnd"); + return 1; + } + printf("Message sent: %s\n", msg.mtext); + + // Check the queue status again + memset(&buf, 0, sizeof(buf)); // Clear the structure + if (msgctl(msqid, IPC_STAT, &buf) == -1) { + perror("msgctl"); + return 1; + } + + printf("\n=== Queue status after the message is sent ===\n"); + print_msqid_ds(&buf); + } + + // Change permissions + buf.msg_perm.mode = 0600; // New permissions + + if (msgctl(msqid, IPC_SET, &buf) == -1) { + perror("msgctl IPC_SET failed"); + msgctl(msqid, IPC_RMID, NULL); + exit(EXIT_FAILURE); + } + + if ((buf.msg_stime - ts.tv_sec > 60) || (ts.tv_sec - buf.msg_stime > 60)) { + printf("\nMsgctl get a error time! \n"); + exit(EXIT_FAILURE); + } + + msgctl(msqid, IPC_RMID, NULL); + + // Restore system time + clock_gettime(CLOCK_REALTIME, &ts_final); + ts_init.tv_sec = ts_init.tv_sec + ts_final.tv_sec - ts.tv_sec; + clock_settime(CLOCK_REALTIME, &ts_init); + + return 0; +} diff --git a/test/misc/tst-rlimit.c b/test/misc/tst-rlimit.c new file mode 100644 index 0000000..84aa465 --- /dev/null +++ b/test/misc/tst-rlimit.c @@ -0,0 +1,157 @@ +/* + * setrlimit/getrlimit/prlimit and setrlimit64/getrlimit64/prlimit64 functions + * test for uClibc. + * + * The prlimit64 function is not called directly in this test, because it is a + * new function for uclibc and can cause build problems with uclibc-ng <= 1.0.44. + * With _FILE_OFFSET_BITS == 64 the prlimit call is redirected to the prlimit64 + * call. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/resource.h> + +#define __ASSERT(x, f, l) \ + if (!(x)) { \ + fprintf(stderr, "%s: LINE %d: ASSERT: " #x "\n", f, l); \ + exit(1); \ + } + +#define ASSERT(x) __ASSERT(x, __FILE__, __LINE__) + +static int resources[] = { + RLIMIT_CORE, + RLIMIT_CPU, + RLIMIT_DATA, + RLIMIT_FSIZE, + RLIMIT_NOFILE, + RLIMIT_STACK, + RLIMIT_AS +}; + +#define nresources (sizeof (resources) / sizeof (resources[0])) +#define TEST_VAL 0x11223344 + + +static void test_getrlimit(int rnum, rlim_t exp_cur, rlim_t exp_max) +{ + struct rlimit r; + + ASSERT(getrlimit(rnum, &r) == 0); + ASSERT(r.rlim_cur == exp_cur); + ASSERT(r.rlim_max == exp_max); +} + +static void test_setrlimit(int rnum, rlim_t new_cur, rlim_t new_max) +{ + struct rlimit r = {.rlim_cur = new_cur, .rlim_max = new_max}; + + ASSERT(setrlimit(rnum, &r) == 0); +} + +static void test_prlimit_get(int rnum, rlim_t exp_cur, rlim_t exp_max) +{ + struct rlimit r; + + ASSERT(prlimit(0, rnum, NULL, &r) == 0); + ASSERT(r.rlim_cur == exp_cur); + ASSERT(r.rlim_max == exp_max); +} + +static void test_prlimit_set(int rnum, rlim_t new_cur, rlim_t new_max) +{ + struct rlimit r = {.rlim_cur = new_cur, .rlim_max = new_max}; + ASSERT(prlimit(0, rnum, &r, NULL) == 0); +} + +#if defined(__USE_LARGEFILE64) +static void test_getrlimit64(int rnum, rlim64_t exp_cur, rlim64_t exp_max) +{ + struct rlimit64 r; + + ASSERT(getrlimit64(rnum, &r) == 0); + ASSERT(r.rlim_cur == exp_cur); + ASSERT(r.rlim_max == exp_max); +} + +static void test_setrlimit64(int rnum, rlim64_t new_cur, rlim64_t new_max) +{ + struct rlimit64 r = {.rlim_cur = new_cur, .rlim_max = new_max}; + + ASSERT(setrlimit64(rnum, &r) == 0); +} +#endif + +int main(void) +{ + int rnum = -1; + struct rlimit rlim; + int i, ret; + + /* Find a resource with hard limit set to infinity */ + for (i = 0; i < nresources; ++i) { + ret = getrlimit(resources[i], &rlim); + if ((!ret) && (rlim.rlim_max == RLIM_INFINITY)) { + rnum = resources[i]; + break; + } + } + + /* Can't continue, return unsupported */ + if (rnum == -1) + return 23; + + /* Test cases */ + test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY); + test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY); + test_setrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_prlimit_set(rnum, TEST_VAL, RLIM_INFINITY); + test_prlimit_get(rnum, TEST_VAL, RLIM_INFINITY); + test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY); + test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY); + +#if defined(__USE_LARGEFILE64) + /* Check setrlim64 and getrlim64 in 32-bit offset LFS environment */ + test_setrlimit64(rnum, TEST_VAL, RLIM64_INFINITY); + test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY); + test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY); + test_setrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY); + test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY); + + test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY); + test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY); + test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY); +#endif + + +#if defined(__USE_LARGEFILE64) && _FILE_OFFSET_BITS == 64 + /* Check setrlim64/getrlim64/prlimit64 in 64-bit offset environment */ + test_setrlimit64(rnum, TEST_VAL, RLIM64_INFINITY); + test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY); + test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY); + test_setrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY); + test_prlimit_get(rnum, RLIM64_INFINITY, RLIM64_INFINITY); + test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_prlimit_set(rnum, TEST_VAL, RLIM64_INFINITY); + test_prlimit_get(rnum, TEST_VAL, RLIM64_INFINITY); + test_prlimit_set(rnum, RLIM64_INFINITY, RLIM64_INFINITY); + test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY); + test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY); + + test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY); + test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY); + test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY); + test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY); +#endif + + return 0; +} diff --git a/test/misc/tst-rlimit64.c b/test/misc/tst-rlimit64.c new file mode 100644 index 0000000..490c6c5 --- /dev/null +++ b/test/misc/tst-rlimit64.c @@ -0,0 +1 @@ +#include "tst-rlimit.c" diff --git a/test/misc/tst-semctl.c b/test/misc/tst-semctl.c new file mode 100644 index 0000000..99647d1 --- /dev/null +++ b/test/misc/tst-semctl.c @@ -0,0 +1,97 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/sem.h> +#include <time.h> +#include <unistd.h> + +union semun { + int val; + struct semid_ds *buf; + unsigned short *array; +}; + +struct timespec ts = { + .tv_sec = 3468960000, // 3468960000 2075-12-05 Destination timestamp + .tv_nsec = 0 +}; + +void print_semid_ds(struct semid_ds *ds) { + printf("sem_perm.uid: %d\n", ds->sem_perm.uid); + printf("sem_perm.gid: %d\n", ds->sem_perm.gid); + printf("sem_perm.cuid: %d\n", ds->sem_perm.cuid); + printf("sem_perm.cgid: %d\n", ds->sem_perm.cgid); + printf("sem_perm.mode: %o\n", ds->sem_perm.mode); + printf("sem_nsems: %d\n", ds->sem_nsems); + printf("sem_otime: %s", ctime(&ds->sem_otime)); + printf("sem_ctime: %s \n", ctime(&ds->sem_ctime)); +} + +int main() { + int semid; + union semun arg; + struct semid_ds ds; + struct timespec ts_init, ts_final; + + // Save system time + if (clock_gettime(CLOCK_REALTIME, &ts_init) == -1) { + perror("Error getting time"); + return 1; + } + + + if (clock_settime(CLOCK_REALTIME, &ts) == -1) { // Set the time to after 2038 + perror("Error setting time"); + return 1; + } + + // Create a semaphore set + if ((semid = semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT)) == -1) { + perror("semget failed"); + exit(1); + } + + // Get the semid_ds structure + arg.buf = &ds; + if (semctl(semid, 0, IPC_STAT, arg) == -1) { + perror("semctl IPC_STAT failed"); + exit(1); + } + + // Print the structure contents + printf("=== semid_ds structure values ===\n"); + print_semid_ds(&ds); + + + // Change permissions + ds.sem_perm.mode = 0600; // Change to new permissions + + if (semctl(semid, 0, IPC_SET, arg) == -1) { + perror("semctl IPC_SET failed"); + semctl(semid, 0, IPC_RMID); + exit(EXIT_FAILURE); + } + + // Print the structure contents + printf("=== semid_ds structure values ===\n"); + print_semid_ds(&ds); + + if ((ds.sem_ctime - ts.tv_sec > 60) || (ts.tv_sec - ds.sem_ctime > 60)) { + printf("\nSemctl get a error time! \n"); + exit(EXIT_FAILURE); + } + + // Delete a semaphore + if (semctl(semid, 0, IPC_RMID) == -1) { + perror("semctl IPC_RMID failed"); + exit(1); + } + + // Restore system time + clock_gettime(CLOCK_REALTIME, &ts_final); + ts_init.tv_sec = ts_init.tv_sec + ts_final.tv_sec - ts.tv_sec; + clock_settime(CLOCK_REALTIME, &ts_init); + + return 0; +} diff --git a/test/misc/tst-shmctl.c b/test/misc/tst-shmctl.c new file mode 100644 index 0000000..36dda83 --- /dev/null +++ b/test/misc/tst-shmctl.c @@ -0,0 +1,84 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/ipc.h> +#include <sys/shm.h> +#include <time.h> +#include <unistd.h> + +struct timespec ts = { + .tv_sec = 3468960000, // 2075-12-05 Destination timestamp + .tv_nsec = 0 +}; + +void print_shmid_ds(struct shmid_ds *buf) { + printf("shm_perm.uid: %d \n", buf->shm_perm.uid); + printf("shm_perm.gid: %d \n", buf->shm_perm.gid); + printf("shm_perm.cuid: %d \n", buf->shm_perm.cuid); + printf("shm_perm.cgid: %d \n", buf->shm_perm.cgid); + printf("shm_perm.mode: %o \n", buf->shm_perm.mode); + printf("shm_segsz: %lu \n", buf->shm_segsz); + printf("shm_lpid: %d \n", buf->shm_lpid); + printf("shm_cpid: %d \n", buf->shm_cpid); + printf("shm_nattch: %lu \n", buf->shm_nattch); + printf("shm_atime: %s", buf->shm_atime ? ctime(&buf->shm_atime) : "Not set\n"); + printf("shm_dtime: %s", buf->shm_dtime ? ctime(&buf->shm_dtime) : "Not set\n"); + printf("shm_ctime: %s\n", ctime(&buf->shm_ctime)); +} + +int main() { + + struct timespec ts_init, ts_final; + + // Save system time + if (clock_gettime(CLOCK_REALTIME, &ts_init) == -1) { + perror("Error getting time"); + return 1; + } + + if (clock_settime(CLOCK_REALTIME, &ts) == -1) { // Set the time to after 2038 + perror("Error setting time"); + return 1; + } + + key_t key = ftok(".", 'S'); + int shmid = shmget(key, 1024, IPC_CREAT | 0666); + if (shmid == -1) { + perror("shmget"); + exit(1); + } + + struct shmid_ds buf; + if (shmctl(shmid, IPC_STAT, &buf) == -1) { + perror("shmctl"); + exit(1); + } + + printf("Shared Memory Segment Info:\n"); + print_shmid_ds(&buf); + + // Change to new permissions + buf.shm_perm.mode = 0600; + + if (shmctl(shmid, IPC_SET, &buf) == -1) { + perror("shmctl IPC_SET failed"); + shmctl(shmid, IPC_RMID, NULL); + exit(EXIT_FAILURE); + } + + if ((buf.shm_ctime - ts.tv_sec > 60) || (ts.tv_sec - buf.shm_ctime > 60)) { + printf("\nShmctl get a error time! \n"); + exit(EXIT_FAILURE); + } + + printf("Shared Memory Segment Info:\n"); + print_shmid_ds(&buf); + + shmctl(shmid, IPC_RMID, NULL); + + // Restore system time + clock_gettime(CLOCK_REALTIME, &ts_final); + ts_init.tv_sec = ts_init.tv_sec + ts_final.tv_sec - ts.tv_sec; + clock_settime(CLOCK_REALTIME, &ts_init); + + return 0; +} |
