From bf8d5be3f192786779bbe2b025c4d464d751e6e4 Mon Sep 17 00:00:00 2001 From: Samuel Pomeroy <1690@holbertonschool.com> Date: Tue, 23 Nov 2021 00:47:31 +0000 Subject: [PATCH] 0x03-0x06 update README to new format; add test mains --- 0x03-proc_filesystem/README.md | 69 +++++++- 0x04-readelf/README.md | 79 ++++++++- 0x05-libasm/README.md | 248 ++++++++++++++++++++++++++++- 0x05-libasm/tests/0-main.c | 25 +++ 0x05-libasm/tests/1-main.c | 25 +++ 0x05-libasm/tests/10-main.c | 32 ++++ 0x05-libasm/tests/100-main.c | 29 ++++ 0x05-libasm/tests/101-main.c | 31 ++++ 0x05-libasm/tests/11-main.c | 32 ++++ 0x05-libasm/tests/12-main.c | 31 ++++ 0x05-libasm/tests/2-main.c | 25 +++ 0x05-libasm/tests/3-main.c | 26 +++ 0x05-libasm/tests/4-main.c | 25 +++ 0x05-libasm/tests/5-main.c | 29 ++++ 0x05-libasm/tests/6-main.c | 23 +++ 0x05-libasm/tests/7-main.c | 21 +++ 0x05-libasm/tests/8-main.c | 27 ++++ 0x05-libasm/tests/9-main.c | 30 ++++ 0x06-signals/README.md | 238 ++++++++++++++++++++++++++- 0x06-signals/tests/0-main.c | 27 ++++ 0x06-signals/tests/1-main.c | 37 +++++ 0x06-signals/tests/10-main.c | 33 ++++ 0x06-signals/tests/100-main.c | 25 +++ 0x06-signals/tests/101-main.c | 53 ++++++ 0x06-signals/tests/102-main.c | 33 ++++ 0x06-signals/tests/103-main.c | 47 ++++++ 0x06-signals/tests/104-main.c | 55 +++++++ 0x06-signals/tests/2-main.c | 27 ++++ 0x06-signals/tests/3-main.c | 37 +++++ 0x06-signals/tests/4-main.c | 27 ++++ 0x06-signals/tests/7-main.c | 22 +++ 0x06-signals/tests/8-main.c | 22 +++ 0x06-signals/{ => tests}/9-main.sh | 0 README.md | 14 +- 34 files changed, 1496 insertions(+), 8 deletions(-) create mode 100644 0x05-libasm/tests/0-main.c create mode 100644 0x05-libasm/tests/1-main.c create mode 100644 0x05-libasm/tests/10-main.c create mode 100644 0x05-libasm/tests/100-main.c create mode 100644 0x05-libasm/tests/101-main.c create mode 100644 0x05-libasm/tests/11-main.c create mode 100644 0x05-libasm/tests/12-main.c create mode 100644 0x05-libasm/tests/2-main.c create mode 100644 0x05-libasm/tests/3-main.c create mode 100644 0x05-libasm/tests/4-main.c create mode 100644 0x05-libasm/tests/5-main.c create mode 100644 0x05-libasm/tests/6-main.c create mode 100644 0x05-libasm/tests/7-main.c create mode 100644 0x05-libasm/tests/8-main.c create mode 100644 0x05-libasm/tests/9-main.c create mode 100644 0x06-signals/tests/0-main.c create mode 100644 0x06-signals/tests/1-main.c create mode 100644 0x06-signals/tests/10-main.c create mode 100644 0x06-signals/tests/100-main.c create mode 100644 0x06-signals/tests/101-main.c create mode 100644 0x06-signals/tests/102-main.c create mode 100644 0x06-signals/tests/103-main.c create mode 100644 0x06-signals/tests/104-main.c create mode 100644 0x06-signals/tests/2-main.c create mode 100644 0x06-signals/tests/3-main.c create mode 100644 0x06-signals/tests/4-main.c create mode 100644 0x06-signals/tests/7-main.c create mode 100644 0x06-signals/tests/8-main.c rename 0x06-signals/{ => tests}/9-main.sh (100%) mode change 100644 => 100755 diff --git a/0x03-proc_filesystem/README.md b/0x03-proc_filesystem/README.md index e4fa32b..c8aeefc 100644 --- a/0x03-proc_filesystem/README.md +++ b/0x03-proc_filesystem/README.md @@ -1 +1,68 @@ -# 0x03. Python - /proc filesystem +# (360) 0x03. Python - /proc filesystem +Specializations > System programming & Algorithm > Linux Programming + +### Project author +Alexandre Gautier + +### Assignment dates +01-12-2021 to 01-22-2021 + +### Description +Introduction to using the `/proc` filesystem to explore the memory footprint of a running process. + +### Requirements +* All your modules should have documentation (`python3 -c 'print(__import__("my_module").__doc__)'`) +* All your classes should have documentation (`python3 -c 'print(__import__("my_module").MyClass.__doc__)'`) +* All your functions (inside and outside a class) should have documentation (`python3 -c 'print(__import__("my_module").my_function.__doc__)'` and `python3 -c 'print(__import__("my_module").MyClass.my_function.__doc__)')` + + +### Provided file(s) + +--- + +## Mandatory Tasks + +### :white_check_mark: 0. Hack the VM +Write a script that finds a string in the heap of a running process, and replaces it. + +* Usage: `read_write_heap.py pid search_string replace_string` + * where `pid` is the pid of the running process + * and strings are ASCII +* The script should look only in the heap of the process +* Output: you can print whatever you think is interesting +* On usage error, print an error message on `stdout` and exit with status code 1 + +File(s): [`read_write_heap.py`](./read_write_heap.py) + +### :white_large_square: 1. Blog post #1 +Write a blog post about the `/proc` filesystem and the `/proc/maps` and `/proc/mem` files. + +Try to explain how to parse the `/proc/maps` file in order to read the virtual memory. + +Your posts should have examples and at least one picture, at the top. Publish your blog post on Medium or LinkedIn, and share it at least on Twitter and LinkedIn. + + + +## Advanced Tasks + +### :white_large_square: 2. Blog post #2 +Write a blog post about the virtual memory. + +How does the virtual memory map into the RAM? + +Try to go as deep as possible in your explanation, and try to explain how the Kernel handles it. + +Your posts should have examples and at least one picture, at the top. Publish your blog post on Medium or LinkedIn, and share it at least on Twitter and LinkedIn. + + + +--- + +## Student +* **Samuel Pomeroy** - [allelomorph](github.com/allelomorph) diff --git a/0x04-readelf/README.md b/0x04-readelf/README.md index c427c7d..4a800fb 100644 --- a/0x04-readelf/README.md +++ b/0x04-readelf/README.md @@ -1 +1,78 @@ -# 0x04. C - ELF: readelf +# (364) 0x04. C - ELF: readelf +Specializations > System programming & Algorithm > Linux Programming + +### Project author +Alexandre Gautier + +### Assignment dates +01-21-2021 to 01-29-2021 + +### Description +Introduction to ELF or the Executable and Linkable Format, its uses, and structure. + +### Requirements +Allowed Functions and System Calls + +* Unless specified otherwise, you are allowed to use the C standard library +* You’re not allowed to use `system`(3) +* You’re not allowed to use `exec`(2) and `exec`(3) + +Tests + +* Your program must be able to handle both 32-bit and 64-bit ELF files +* Your program must be able to handle both little and big endian ELF files +* Your program must be able to handle all types of ELF files + + +### Provided file(s) + +--- + +## Mandatory Tasks + +### :white_check_mark: 0. ELF file header +Write a program that displays the information contained in the ELF file header of an ELF file. + +* Usage: `0-hreadelf elf_filename` +* Your standard output, error output and status should be the exact same as `readelf -W -h` + +Your makefile must define the rule `0-hreadelf` and compile the needed sources to form the executable `0-hreadelf` + +Compiled: `make 0-hreadelf` + +### :white_check_mark: 1. ELF sections' headers +Write a program that displays the information contained in the ELF sections' headers of an ELF file. + +* Usage: `1-hreadelf elf_filename` +* Your standard output, error output and status should be the exact same as `readelf -W -S` + +Your makefile must define the rule `1-hreadelf` and compile the needed sources to form the executable `1-hreadelf` + +Compiled: `make 1-hreadelf` + +### :white_check_mark: 2. ELF program headers +Write a program that displays the information contained in the ELF program headers of an ELF file. + +* Usage: `2-hreadelf elf_filename` +* Your standard output, error output and status should be the exact same as `readelf -W -l` + +Your makefile must define the rule `2-hreadelf` and compile the needed sources to form the executable `2-hreadelf` + +Compiled: `make 2-hreadelf` + +## Advanced Tasks + +### :white_check_mark: 3. ELF symbol table +Write a program that displays the information contained in the ELF symbol tables of an ELF file. + +* Usage: `100-hreadelf elf_filename` +* Your standard output, error output and status should be the exact same as `readelf -W -s` + +Your makefile must define the rule `100-hreadelf` and compile the needed sources to form the executable `100-hreadelf` + +Compiled: `make 100-hreadelf` + +--- + +## Student +* **Samuel Pomeroy** - [allelomorph](github.com/allelomorph) diff --git a/0x05-libasm/README.md b/0x05-libasm/README.md index 04d808c..71df86f 100644 --- a/0x05-libasm/README.md +++ b/0x05-libasm/README.md @@ -1 +1,247 @@ -# 0x05. x86 Assembly - libASM +# (376) 0x05. x86 Assembly - libASM +Specializations > System programming & Algorithm > Linux Programming + +### Project author +Alexandre Gautier + +### Assignment dates +02-01-2021 to 02-12-2021 + +### Description +Introduction to x86 assembly: Intel vs AT&T, flag registers, stack frames, function and system calls. + +### Requirements +* Allowed Functions and System Calls +Unless specified otherwise, you are NOT allowed to do any call or make any system call. It means you’re not allowed to use either the `call` nor the `syscall` instructions. +* ASM programs and functions expected to be compiled with NASM version 2.10.09 using the flags `-f elf64` + +### Provided file(s) +* [`0-main.c`](./tests/0-main.c) [`1-main.c`](./tests/1-main.c) [`2-main.c`](./tests/2-main.c) [`3-main.c`](./tests/3-main.c) [`4-main.c`](./tests/4-main.c) [`5-main.c`](./tests/5-main.c) [`6-main.c`](./tests/6-main.c) [`7-main.c`](./tests/7-main.c) [`8-main.c`](./tests/8-main.c) [`9-main.c`](./tests/9-main.c) [`10-main.c`](./tests/10-main.c) [`11-main.c`](./tests/11-main.c) [`12-main.c`](./tests/12-main.c) +* [`100-main.c`](./tests/100-main.c) [`101-main.c`](./tests/101-main.c) + +--- + +## Mandatory Tasks + +### :white_check_mark: 0. strlen +Write a copycat of the function `strlen`(3), in x86-64 Assembly + +* Prototype when used in C: `size_t asm_strlen(const char *str);` + +File(s): [`0-strlen.asm`](./0-strlen.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 0-main.o 0-main.c +$ nasm -f elf64 -o 0-strlen.o 0-strlen.asm +$ gcc -o 0-strlen 0-main.o 0-strlen.o +``` + +### :white_check_mark: 1. strcmp +Write a copycat of the function `strcmp`(3), in x86-64 Assembly + +* Prototype when used in C: `int asm_strcmp(const char *s1, const char *s2);` + +File(s): [`1-strcmp.asm`](./1-strcmp.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 1-main.o 1-main.c +$ nasm -f elf64 -o 1-strcmp.o 1-strcmp.asm +$ gcc -o 1-strcmp 1-main.o 1-strcmp.o +``` + +### :white_check_mark: 2. strncmp +Write a copycat of the function `strncmp`(3), in x86-64 Assembly + +* Prototype when used in C: `int asm_strncmp(const char *s1, const char *s2, size_t n);` + +File(s): [`2-strncmp.asm`](./2-strncmp.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 2-main.o 2-main.c +$ nasm -f elf64 -o 2-strncmp.o 2-strncmp.asm +$ gcc -o 2-strncmp 2-main.o 2-strncmp.o +``` + +### :white_check_mark: 3. strchr +Write a copycat of the function `strchr`(3), in x86-64 Assembly + +* Prototype when used in C: `char *asm_strchr(const char *s, int c);` + +File(s): [`3-strchr.asm`](./3-strchr.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 3-main.o 3-main.c +$ nasm -f elf64 -o 3-strchr.o 3-strchr.asm +$ gcc -o 3-strchr 3-main.o 3-strchr.o +``` + +### :white_check_mark: 4. strstr +Write a copycat of the function `strstr`(3), in x86-64 Assembly + +* Prototype when used in C: `char *asm_strstr(const char *haystack, const char *needle);` + +File(s): [`4-strstr.asm`](./4-strstr.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 4-main.o 4-main.c +$ nasm -f elf64 -o 4-strstr.o 4-strstr.asm +$ gcc -o 4-strstr 4-main.o 4-strstr.o +``` + +### :white_check_mark: 5. memcpy +Write a copycat of the function `memcpy`(3), in x86-64 Assembly + +* Prototype when used in C: `void *asm_memcpy(void *dest, const void *src, size_t n);` + +File(s): [`5-memcpy.asm`](./5-memcpy.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 5-main.o 5-main.c +$ nasm -f elf64 -o 5-memcpy.o 5-memcpy.asm +$ gcc -o 5-memcpy 5-main.o 5-memcpy.o +``` + +### :white_check_mark: 6. putc +Write a function that prints a single character on the standard output, in x86-64 Assembly + +* Prototype when used in C: `size_t asm_putc(int c);` +* Where `c` holds the character to be printed +* Your function must return the total number of bytes written on the standard output +* For this task, you are allowed to use the `syscall` instruction only once in your file + +File(s): [`6-putc.asm`](./6-putc.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 6-main.o 6-main.c +$ nasm -f elf64 -o 6-putc.o 6-putc.asm +$ gcc -o 6-putc 6-main.o 6-putc.o +``` + +### :white_check_mark: 7. puts +Write a function that prints a string of characters on the standard output, in x86-64 Assembly + +* Prototype when used in C: size_t `asm_puts(const char *str);` +* Where `str` holds the string to be printed +* Your function must return the total number of bytes written on the standard output +* You are not allowed to use any sort of `jump` +* Your file `0-strlen.asm` will be compiled as well, you are allowed to `call` it once in your file +* For this task, you are allowed to use the `syscall` instruction only once in your file + +File(s): [`7-puts.asm`](./7-puts.asm) [`0-strlen.asm`](./0-strlen.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 7-main.o 7-main.c +$ nasm -f elf64 -o 7-puts.o 7-puts.asm +$ nasm -f elf64 -o 0-strlen.o 0-strlen.asm +$ gcc -o 7-puts 7-main.o 7-puts.o 0-strlen.o +``` + +### :white_check_mark: 8. strcasecmp +Write a copycat of the function `strcasecmp`(3), in x86-64 Assembly + +* Prototype when used in C: `int asm_strcasecmp(const char *s1, const char *s2);` + +File(s): [`8-strcasecmp.asm`](./8-strcasecmp.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 8-main.o 8-main.c +$ nasm -f elf64 -o 8-strcasecmp.o 8-strcasecmp.asm +$ gcc -o 8-strcasecmp 8-main.o 8-strcasecmp.o +``` + +### :white_check_mark: 9. strncasecmp +Write a copycat of the function `strncasecmp`(3), in x86-64 Assembly + +* Prototype when used in C: `int asm_strncasecmp(const char *s1, const char *s2, size_t n);` + +File(s): [`9-strncasecmp.asm`](./9-strncasecmp.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 9-main.o 9-main.c +$ nasm -f elf64 -o 9-strncasecmp.o 9-strncasecmp.asm +$ gcc -o 9-strncasecmp 9-main.o 9-strncasecmp.o +``` + +### :white_check_mark: 10. strspn +Write a copycat of the function `strspn`(3), in x86-64 Assembly + +* Prototype when used in C: `size_t asm_strspn(const char *s, const char *accept);` + +File(s): [`10-strspn.asm`](./10-strspn.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 10-main.o 10-main.c +$ nasm -f elf64 -o 10-strspn.o 10-strspn.asm +$ gcc -o 10-strspn 10-main.o 10-strspn.o +``` + +### :white_check_mark: 11. strcspn +Write a copycat of the function `strcspn`(3), in x86-64 Assembly + +* Prototype when used in C: `size_t asm_strcspn(const char *s, const char *reject);` + +File(s): [`11-strcspn.asm`](./11-strcspn.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 11-main.o 11-main.c +$ nasm -f elf64 -o 11-strcspn.o 11-strcspn.asm +$ gcc -o 11-strcspn 11-main.o 11-strcspn.o +``` + +### :white_check_mark: 12. strpbrk +Write a copycat of the function `strpbrk`(3), in x86-64 Assembly + +* Prototype when used in C: `char *asm_strpbrk(const char *s, const char *accept);` + +File(s): [`12-strpbrk.asm`](./12-strpbrk.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 12-main.o 12-main.c +$ nasm -f elf64 -o 12-strpbrk.o 12-strpbrk.asm +$ gcc -o 12-strpbrk 12-main.o 12-strpbrk.o +``` + +## Advanced Tasks + +### :white_check_mark: 13. puti +Write a function that prints a signed integer on the standard output, in x86-64 Assembly + +* Prototype when used in C: `size_t asm_puti(int n);` +* Where `n` holds the integer to be printed +* Your function must return the total number of bytes written on the standard output +* Your file `6-putc.asm` will be compiled as well +* You are allowed to use the instruction `call` up to three times in your file + +File(s): [`100-puti.asm`](./100-puti.asm) [`6-putc.asm`](./6-putc.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 100-main.o 100-main.c +$ nasm -f elf64 -o 100-puti.o 100-puti.asm +$ nasm -f elf64 -o 6-putc.o 6-putc.asm +$ gcc -o 100-puti 100-main.o 100-puti.o 6-putc.o +``` + +### :white_check_mark: 14. puti_base +Write a function that prints a signed integer in a given base on the standard output, in x86-64 Assembly + +* Prototype when used in C: `size_t asm_puti_base(int n, const char *base);` +* Where `n` holds the integer to be printed +* And `base` points to a string representing the base used to print `n` +* Your function must return the total number of bytes written on the standard output +* Your files `6-putc.asm` and `0-strlen.asm` will be compiled as well +* You are allowed to use the instruction `call` up to four times in your file + +File(s): [`101-puti_base.asm`](./101-puti_base.asm) [`6-putc.asm`](./6-putc.asm) [`0-strlen.asm`](./0-strlen.asm)\ +Compiled: +```bash +$ gcc -Wall -Wextra -Werror -pedantic -g3 -c -o 101-main.o 101-main.c +$ nasm -f elf64 -o 101-puti_base.o 101-puti_base.asm +$ nasm -f elf64 -o 6-putc.o 6-putc.asm +$ nasm -f elf64 -o 0-strlen.o 0-strlen.asm +$ gcc -o 101-puti_base 101-main.o 101-puti_base.o 6-putc.o 0-strlen.o +``` + +--- + +## Student +* **Samuel Pomeroy** - [allelomorph](github.com/allelomorph) diff --git a/0x05-libasm/tests/0-main.c b/0x05-libasm/tests/0-main.c new file mode 100644 index 0000000..b14b2f1 --- /dev/null +++ b/0x05-libasm/tests/0-main.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton School" +#define S2 "" +#define S3 "\0" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strlen(S1) == asm_strlen(S1)); + assert(strlen(S2) == asm_strlen(S2)); + assert(strlen(S3) == asm_strlen(S3)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/1-main.c b/0x05-libasm/tests/1-main.c new file mode 100644 index 0000000..a986ecd --- /dev/null +++ b/0x05-libasm/tests/1-main.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton School" +#define S2 "" +#define S3 "Holberton Socool" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strcmp(S1, S1) == asm_strcmp(S1, S1)); + assert(strcmp(S1, S2) == asm_strcmp(S1, S2)); + assert(strcmp(S1, S3) == asm_strcmp(S1, S3)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/10-main.c b/0x05-libasm/tests/10-main.c new file mode 100644 index 0000000..7ab8b18 --- /dev/null +++ b/0x05-libasm/tests/10-main.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton" +#define S2 "holberton" +#define S3 "HOLBERTON" + +#define A1 "abcdefghijklmnopqrstuvwxyz" +#define A2 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strspn(S2, A1) == asm_strspn(S2, A1)); + assert(strspn(S2, A2) == asm_strspn(S2, A2)); + assert(strspn(S3, A1) == asm_strspn(S3, A1)); + assert(strspn(S3, A2) == asm_strspn(S3, A2)); + assert(strspn(S1, A1) == asm_strspn(S1, A1)); + assert(strspn(S1, A2) == asm_strspn(S1, A2)); + assert(strspn(S1, A1 A2) == asm_strspn(S1, A1 A2)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/100-main.c b/0x05-libasm/tests/100-main.c new file mode 100644 index 0000000..8c89dea --- /dev/null +++ b/0x05-libasm/tests/100-main.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +#include "libasm.h" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(asm_puti(98) == 2); + printf("\n"); + assert(asm_puti(-1) == 2); + printf("\n"); + assert(asm_puti(-1024) == 5); + printf("\n"); + assert(asm_puti(INT_MAX) == 10); + printf("\n"); + assert(asm_puti(INT_MIN) == 11); + printf("\n"); + assert(asm_puti(0) == 1); + printf("\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/101-main.c b/0x05-libasm/tests/101-main.c new file mode 100644 index 0000000..274d41f --- /dev/null +++ b/0x05-libasm/tests/101-main.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +#include "libasm.h" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + asm_puti_base(98, "0123456789"); + printf("\n"); + asm_puti_base(-1, "0123456789"); + printf("\n"); + asm_puti_base(129342, "0123456789abcdef"); + printf("\n"); + asm_puti_base(781532, "01"); + printf("\n"); + asm_puti_base(INT_MAX, "abcdefghijklmnopqrstwxyz"); + printf("\n"); + asm_puti_base(INT_MIN, "0123456789ABCDEF"); + printf("\n"); + asm_puti_base(0, "01234567"); + printf("\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/11-main.c b/0x05-libasm/tests/11-main.c new file mode 100644 index 0000000..cdb23cb --- /dev/null +++ b/0x05-libasm/tests/11-main.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton" +#define S2 "holberton" +#define S3 "HOLBERTON" + +#define A1 "abcdefghijklmnopqrstuvwxyz" +#define A2 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strcspn(S2, A1) == asm_strcspn(S2, A1)); + assert(strcspn(S2, A2) == asm_strcspn(S2, A2)); + assert(strcspn(S3, A1) == asm_strcspn(S3, A1)); + assert(strcspn(S3, A2) == asm_strcspn(S3, A2)); + assert(strcspn(S1, A1) == asm_strcspn(S1, A1)); + assert(strcspn(S1, A2) == asm_strcspn(S1, A2)); + assert(strcspn(S1, A1 A2) == asm_strcspn(S1, A1 A2)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/12-main.c b/0x05-libasm/tests/12-main.c new file mode 100644 index 0000000..b0fa86a --- /dev/null +++ b/0x05-libasm/tests/12-main.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton" +#define S2 "holberton" +#define S3 "HOLBERTON" + +#define A1 "abcdefghijklmnopqrstuvwxyz" +#define A2 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +#define A3 "()[]{}<>n" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strpbrk(S2, A1) == asm_strpbrk(S2, A1)); + assert(strpbrk(S2, A2) == asm_strpbrk(S2, A2)); + assert(strpbrk(S3, A1) == asm_strpbrk(S3, A1)); + assert(strpbrk(S3, A2) == asm_strpbrk(S3, A2)); + assert(strpbrk(S1, A3) == asm_strpbrk(S1, A3)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/2-main.c b/0x05-libasm/tests/2-main.c new file mode 100644 index 0000000..5b1cdce --- /dev/null +++ b/0x05-libasm/tests/2-main.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton School" +#define S2 "" +#define S3 "Holberton Socool" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strncmp(S1, S3, 1) == asm_strncmp(S1, S3, 1)); + assert(strncmp(S1, S3, 11) == asm_strncmp(S1, S3, 11)); + assert(strncmp(S1, S3, 15) == asm_strncmp(S1, S3, 15)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/3-main.c b/0x05-libasm/tests/3-main.c new file mode 100644 index 0000000..484b27b --- /dev/null +++ b/0x05-libasm/tests/3-main.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton School" +#define C1 'n' +#define C2 'S' +#define C3 's' + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strchr(S1, C1) == asm_strchr(S1, C1)); + assert(strchr(S1, C2) == asm_strchr(S1, C2)); + assert(strchr(S1, C3) == asm_strchr(S1, C3)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/4-main.c b/0x05-libasm/tests/4-main.c new file mode 100644 index 0000000..eedb0d0 --- /dev/null +++ b/0x05-libasm/tests/4-main.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton School" +#define S2 "School" +#define S3 "Socool" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strstr(S1, S2) == asm_strstr(S1, S2)); + assert(strstr(S1, S3) == asm_strstr(S1, S3)); + assert(strstr(S1, S1) == asm_strstr(S1, S1)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/5-main.c b/0x05-libasm/tests/5-main.c new file mode 100644 index 0000000..a7213c5 --- /dev/null +++ b/0x05-libasm/tests/5-main.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +#include "libasm.h" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + int i; + char *src = strdup("Holberton"); + + for (i = 0; i <= 9; i++) + { + char *dest = strdup("......... School"); + + assert(asm_memcpy(dest, src, i) == dest); + printf("%s\n", dest); + free(dest); + } + free(src); + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/6-main.c b/0x05-libasm/tests/6-main.c new file mode 100644 index 0000000..5d7aa93 --- /dev/null +++ b/0x05-libasm/tests/6-main.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +#include "libasm.h" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(asm_putc('H') == 1); + assert(asm_putc('b') == 1); + assert(asm_putc('t') == 1); + assert(asm_putc('n') == 1); + assert(asm_putc('\n') == 1); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/7-main.c b/0x05-libasm/tests/7-main.c new file mode 100644 index 0000000..b83465f --- /dev/null +++ b/0x05-libasm/tests/7-main.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +#include "libasm.h" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(asm_puts("H") == 1); + assert(asm_puts("olberton") == 8); + assert(asm_puts(" School\n") == 8); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/8-main.c b/0x05-libasm/tests/8-main.c new file mode 100644 index 0000000..1fbdba8 --- /dev/null +++ b/0x05-libasm/tests/8-main.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton School" +#define S2 "HOLBERTON SCHOOL" +#define S3 "Holberton SchooL" +#define S4 "holberton socool" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strcasecmp(S1, S1) == asm_strcasecmp(S1, S1)); + assert(strcasecmp(S1, S2) == asm_strcasecmp(S1, S2)); + assert(strcasecmp(S1, S3) == asm_strcasecmp(S1, S3)); + assert(strcasecmp(S1, S4) == asm_strcasecmp(S1, S4)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x05-libasm/tests/9-main.c b/0x05-libasm/tests/9-main.c new file mode 100644 index 0000000..160804e --- /dev/null +++ b/0x05-libasm/tests/9-main.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +#include "libasm.h" + +#define S1 "Holberton School" +#define S2 "HOLBERTON SCHOOL" +#define S3 "Holberton SchooL" +#define S4 "holberton socool" + +/** + * main - Program entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + assert(strncasecmp(S1, S3, 1) == asm_strncasecmp(S1, S3, 1)); + assert(strncasecmp(S1, S2, 11) == asm_strncasecmp(S1, S2, 11)); + assert(strncasecmp(S1, S3, 11) == asm_strncasecmp(S1, S3, 11)); + assert(strncasecmp(S1, S2, 16) == asm_strncasecmp(S1, S2, 16)); + assert(strncasecmp(S1, S3, 16) == asm_strncasecmp(S1, S3, 16)); + assert(strncasecmp(S1, S4, 11) == asm_strncasecmp(S1, S4, 11)); + assert(strncasecmp(S1, S4, 16) == asm_strncasecmp(S1, S4, 16)); + + printf("All good!\n"); + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/README.md b/0x06-signals/README.md index 373f606..3a02e12 100644 --- a/0x06-signals/README.md +++ b/0x06-signals/README.md @@ -1 +1,237 @@ -# 0x06. C - Signals +# (362) 0x06. C - Signals +Specializations > System programming & Algorithm > Linux Programming + +### Project author +Alexandre Gautier + +### Assignment dates +02-15-2021 to 02-23-2021 + +### Description +Introduction to Unix signals: sending, listening, and catching. + +### Requirements + +### Provided file(s) +* [`0-main.c`](./tests/0-main.c) [`1-main.c`](./tests/1-main.c) [`2-main.c`](./tests/2-main.c) [`3-main.c`](./tests/3-main.c) [`4-main.c`](./tests/4-main.c) [`7-main.c`](./tests/7-main.c) [`8-main.c`](./tests/8-main.c) [`9-main.sh`](./tests/9-main.sh) [`10-main.c`](./tests/10-main.c) +* [`100-main.c`](./tests/100-main.c) [`101-main.c`](./tests/101-main.c) [`102-main.c`](./tests/102-main.c) [`103-main.c`](./tests/103-main.c) [`104-main.c`](./tests/104-main.c) + +--- + +## Mandatory Tasks + +### :white_check_mark: 0. Handle signal +Write a function that set a handler for the signal `SIGINT` + +* Prototype: `int handle_signal(void);` +* Your function must return 0 on success, or -1 on error +* The program should print `Gotcha! []` followed by a new line, every time Control-C is pressed + * where `` must be replaced with the signal number that was caught +* `sigaction`(2) is not allowed + +File(s): [`0-handle_signal.c`](./0-handle_signal.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 0-main.c 0-handle_signal.c -o 0-handle_signal` + +### :white_check_mark: 1. Current handler - signal +Write a function that retrieves the current handler of the signal `SIGINT` + +* Prototype: `void (*current_handler_signal(void))(int);` +* Your function returns a pointer to the current handler of `SIGINT`, or `NULL` on failure +* You are not allowed to use `sigaction`(2) +* The handler must be unchanged after calling your function + +File(s): [`1-current_handler_signal.c`](./1-current_handler_signal.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 1-main.c 1-set_print_hello.c 1-current_handler_signal.c -o 1-current_handler_signal` + +### :white_check_mark: 2. Gotta catch them all +Write a function that set a handler for the signal `SIGINT` + +* Prototype: `int handle_sigaction(void);` +* Your function must return 0 on success, or -1 on error +* The program should print `Gotcha! []` followed by a new line, every time Control-C is pressed + * where `` must be replaced with the signal number that was caught +* `signal`(2) is not allowed + +File(s): [`2-handle_sigaction.c`](./2-handle_sigaction.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 2-main.c 2-handle_sigaction.c -o 2-handle_sigaction` + +### :white_check_mark: 3. Current handler - sigaction +Write a function that retrieves the current handler of the signal `SIGINT` + +* Prototype: `void (*current_handler_sigaction(void))(int);` +* Your function returns a pointer to the current handler of `SIGINT`, or `NULL` on failure +* You have to use the function `sigaction` (`signal` is not allowed) +* The handler must be unchanged after calling your function + +File(s): [`3-current_handler_sigaction.c`](./3-current_handler_sigaction.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 3-main.c 3-set_print_hello.c 3-current_handler_sigaction.c -o 3-current_handler_sigaction` + +### :white_check_mark: 4. Who said that?! +Write a function that defines a handler for the signal `SIGQUIT` (Control-\ in a shell) + +* Prototype: `int trace_signal_sender(void);` +* The handler must print `SIGQUIT` sent by `` each time a `SIGQUIT` (and only a `SIGQUIT`) is caught + * Where `` must be replaced by the PID of the process that sent the signal +* Your function must return 0 on success, or -1 on error + +File(s): [`4-trace_signal_sender.c`](./4-trace_signal_sender.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 4-main.c 4-trace_signal_sender.c -o 4-trace_signal_sender` + +### :white_check_mark: 5. Description +Write a program that prints a description of a given signal + +* Usage: `./describe ` + * Where `` is the signal number to be described + * If the number of arguments is not correct, your program must print `Usage: %s ` (where `%s` is `argv[0]`), followed by a new line, and exit with `EXIT_FAILURE` +* You’re not allowed to have more than 1 function in your file +* You’re not allowed to have more than 12 lines in your function +* You can assume that if a parameter is given, it will be a number + +File(s): [`5-signal_describe.c`](./5-signal_describe.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 5-signal_describe.c -o 5-signal_describe` + +### :white_check_mark: 6. Catch a single time +Write a program that sets a handler for the signal SIGINT, and exits right after the signal is received and handled + +* Your program does not take any argument +* Your program should suspend indefinitely until a signal is received +* When a `SIGINT` is received, you must print `Caught %d` (where `%d` must be replaced by the signal number), followed by a new line +* After the first `SIGINT` is received, your program must: + * Print `Signal received`, followed by a new line + * Exit with `EXIT_SUCCESS` +* You are not allowed to use the functions `exit`, `sleep` or `_exit` +* You are not allowed to use any kind of loop (`while`, `for`, `do`/`while`) + +File(s): [`6-suspend.c`](./6-suspend.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 6-suspend.c -o 6-suspend` + +### :white_check_mark: 7. Sending a signal +Write a program that sends the signal `SIGINT` to a process, given its PID + +* Usage: `signal_send ` + * Where `` is the PID of the process to send a signal to + * If the number of arguments is not correct, your program must print `Usage: %s ` (where `%s` is `argv[0]`), followed by a new line, and exit with `EXIT_FAILURE` + * `` won’t be 0 +* Your program must return `EXIT_SUCCESS` on success, or `EXIT_FAILURE` +* You’re not allowed to have more than 1 function in your file + +File(s): [`7-signal_send.c`](./7-signal_send.c)\ +Compiled: +* Test: `gcc -Wall -Wextra -Werror -pedantic 7-main.c -o 7-wait_for_it` +* `gcc -Wall -Wextra -Werror -pedantic 7-signal_send.c -o 7-signal_send` + +### :white_check_mark: 8. Sending a signal in a shell +Write a `sh` script that sends the signal `SIGQUIT` to a process, given its PID + +* Usage: `signal_send.sh ` + * Where `` is the PID of the process to send a signal to + * If the number of arguments is not correct, your program must print `Usage: %s ` (where `%s` is `argv[0]`), followed by a new line, and exit with 1 + +File(s): [`8-signal_send.sh`](./8-signal_send.sh)\ +Compiled: Test: `gcc -Wall -Wextra -Werror -pedantic 8-main.c -o 8-wait_for_it` + +### :white_check_mark: 9. Catch with sh +Write a sh script that set a handler for the signals `SIGABRT`, `SIGIO` and `SIGTERM` + +* The script must print `Nope` followed by a new line, each time a `SIGABRT`, `SIGIO` or `SIGTERM` is caught +* Your script must contain a shebang +* You’re not allowed to have more than 2 lines in your script +* You’re not allowed to have more than 1 instruction per line + +File(s): [`9-handle_signal.sh`](./9-handle_signal.sh) + +### :white_check_mark: 10. Does it exist? +Write a function that tests if a process exists, given its PID + +* Prototype: `int pid_exist(pid_t pid);` +* Your function must return 1 if the process with the PID `pid` exists, or 0 otherwise +* You’re not allowed to have more than 1 function in your file +* You’re not allowed to have more than 1 line in your function +* You’re not allowed to include more than 2 headers in your file +* You’re not allowed to include your header file `signals.h` +* You’re not allowed to use the function `getpgid` + +File(s): [`10-pid_exist.c`](./10-pid_exist.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 10-main.c 10-pid_exist.c -o 10-pid_exist` + +### :white_large_square: 11. Blog post +Write a blog post about signals + +Your blog post should cover the following: + +* What is a signal +* Why do they exist +* When are they delivered and by whom +* What are the default actions of signals +* What happens to a process when it receives a signal without handling it +* What happens to a process when it receives a signal and handles it + +Your posts should have examples and at least one picture, at the top. Publish your blog post on Medium or LinkedIn, and share it at least on LinkedIn. + + + +## Advanced Tasks + +### :white_check_mark: 12. I'm saying it's unkillable. +Write a function that sets up a single handler for all the signals + +* Prototype: `void all_in_one(void);` +* You’re not allowed to have more than 2 functions in your file +* You’re not allowed to have more than 10 lines in your functions +* You’re not allowed to use `signal`(2) +* In your handler, you must use the function `psiginfo` to print information about the signal received + +File(s): [`100-all_in_one.c`](./100-all_in_one.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 100-main.c 100-all_in_one.c -o 100-all_in_one` + +### :white_check_mark: 13. Sigset +Write a function that initializes a sigset + +* Prototype: `int sigset_init(sigset_t *set, int *signals);` +* Where `set` is a pointer to the signal set to initialize +* And `signals` is a 0-terminated array of int, each one being a signal number +* Your function must return 0 on success, or -1 on error + +File(s): [`101-sigset_init.c`](./101-sigset_init.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 101-main.c 101-sigset_init.c -o 101-sigset_init` + +### :white_check_mark: 14. Block signals +Write a function that block a given set of signals from being delivered to the current process + +* Prototype: `int signals_block(int *signals);` +* Where `signals` is a 0-terminated array of `int`, each one being a signal to block +* Your function must return 0 on success, or -1 on error +* You’re not allowed to use either `signal`(2) nor `sigaction`(2) + +File(s): [`102-signals_block.c`](./102-signals_block.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 102-main.c 102-signals_block.c -o 102-signals_block` + +### :white_check_mark: 15. Unblock signals +Write a function that unblock a given set of signals from being delivered to the current process + +* Prototype: `int signals_unblock(int *signals);` +* Where `signals` is a 0-terminated array of `int`, each one being a signal to unblock +* Your function must return 0 on success, or -1 on error +* You’re not allowed to use either `signal`(2) nor `sigaction`(2) + +File(s): [`103-signals_unblock.c`](./103-signals_unblock.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 103-main.c 103-signals_unblock.c _signals_block.c -o 103-signals_unblock` + +### :white_check_mark: 16. Handle pending signals +Write a function that sets up a handler for all the pending signals of the current process + +* Prototype: `int handle_pending(void (*handler)(int));` +* Where `handler` is a pointer to the handler function to set for the pending signals +* Your function must return 0 on success, or -1 on error +* You’re not allowed to use `signal`(2) + +File(s): [`104-handle_pending.c`](./104-handle_pending.c)\ +Compiled: `gcc -Wall -Wextra -Werror -pedantic 104-main.c 104-handle_pending.c _signals_block.c _signals_unblock.c -o 104-handle_pending` + +--- + +## Student +* **Samuel Pomeroy** - [allelomorph](github.com/allelomorph) diff --git a/0x06-signals/tests/0-main.c b/0x06-signals/tests/0-main.c new file mode 100644 index 0000000..144cab1 --- /dev/null +++ b/0x06-signals/tests/0-main.c @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "signals.h" + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + int i; + + if (handle_signal() == -1) + { + printf("Failure\n"); + return (EXIT_FAILURE); + } + for (i = 0; ; i++) + { + printf("[%d] Wait for it ...\n", i); + sleep(1); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/1-main.c b/0x06-signals/tests/1-main.c new file mode 100644 index 0000000..87252fd --- /dev/null +++ b/0x06-signals/tests/1-main.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#include "signals.h" + +/* Our functions */ +void print_hello(int); +void set_print_hello(void); + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + void (*handler)(int); + int i; + + handler = current_handler_signal(); + printf("Address of the current handler: %#lx\n", (unsigned long int)handler); + + /* Set 'print_hello()` as the handler for SIGINT */ + set_print_hello(); + + handler = current_handler_signal(); + printf("Address of the 'print_hello' function: %#lx\n", (unsigned long int)&print_hello); + printf("Address of the current handler: %#lx\n", (unsigned long int)handler); + + for (i = 0; ; i++) + { + printf("[%d] Wait for it ...\n", i); + sleep(1); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/10-main.c b/0x06-signals/tests/10-main.c new file mode 100644 index 0000000..e99f6a7 --- /dev/null +++ b/0x06-signals/tests/10-main.c @@ -0,0 +1,33 @@ +#include +#include + +#include "signals.h" + +/** + * main - Entry point + * @argc: Arguments counter + * @argv: Arguments vector + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(int argc, const char *argv[]) +{ + pid_t pid; + + if (argc < 2) + { + fprintf(stderr, "Usage: %s \n", argv[0]); + return (EXIT_FAILURE); + } + pid = atoi(argv[1]); + + if (pid_exist(pid)) + { + printf("PID %d exists\n", pid); + } + else + { + printf("PID %d does not exist\n", pid); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/100-main.c b/0x06-signals/tests/100-main.c new file mode 100644 index 0000000..1021496 --- /dev/null +++ b/0x06-signals/tests/100-main.c @@ -0,0 +1,25 @@ +#include +#include +#include + +#include "signals.h" + +/** + * main - Simple program, printing its PID and running infinitely + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + pid_t pid; + + all_in_one(); + + pid = getpid(); + printf("PID: %d\n", (int)pid); + while (98) + { + sleep(2); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/101-main.c b/0x06-signals/tests/101-main.c new file mode 100644 index 0000000..7f2f61a --- /dev/null +++ b/0x06-signals/tests/101-main.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#include "signals.h" + +/** + * sigset_print - Prints a signal set + * @set: Set to be printed + */ +void sigset_print(const sigset_t *set) +{ + int sig; + int cnt; + + cnt = 0; + for (sig = 1; sig < NSIG; sig++) + { + if (sigismember(set, sig)) + { + cnt++; + printf("%d (%s)\n", sig, strsignal(sig)); + } + } + if (cnt == 0) + printf("Empty signal set\n"); +} + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + sigset_t set; + int signals[] = { + SIGINT, + SIGQUIT, + SIGSEGV, + SIGTRAP, + 0 + }; + + if (sigset_init(&set, signals) == -1) + { + fprintf(stderr, "Failed to init sigset\n"); + return (EXIT_FAILURE); + } + sigset_print(&set); + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/102-main.c b/0x06-signals/tests/102-main.c new file mode 100644 index 0000000..422c075 --- /dev/null +++ b/0x06-signals/tests/102-main.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +#include "signals.h" + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + int signals[] = { + SIGINT, + SIGQUIT, + SIGSEGV, + SIGTRAP, + 0 + }; + + if (signals_block(signals) == -1) + { + fprintf(stderr, "Failed to block signals\n"); + return (EXIT_FAILURE); + } + while (98) + { + sleep(5); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/103-main.c b/0x06-signals/tests/103-main.c new file mode 100644 index 0000000..a8ab1c3 --- /dev/null +++ b/0x06-signals/tests/103-main.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#include "signals.h" + +/* Our function */ +int _signals_block(int *signals); + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + int signals_to_block[] = { + SIGINT, + SIGQUIT, + SIGSEGV, + SIGTRAP, + 0 + }; + int signals_to_unblock[] = { + SIGINT, + SIGSEGV, + SIGTRAP, + 0 + }; + + if (_signals_block(signals_to_block) == -1) + { + fprintf(stderr, "Failed to block signals\n"); + return (EXIT_FAILURE); + } + if (signals_unblock(signals_to_unblock) == -1) + { + fprintf(stderr, "Failed to block signals\n"); + return (EXIT_FAILURE); + } + while (98) + { + sleep(5); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/104-main.c b/0x06-signals/tests/104-main.c new file mode 100644 index 0000000..45dbded --- /dev/null +++ b/0x06-signals/tests/104-main.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#include "signals.h" + +/* Our functions */ +int _signals_block(int *signals); +int _signals_unblock(int *signals); + +/** + * handler - Handler for pending signals + * @signum: Signal number + */ +void handler(int signum) +{ + printf("\nCaught signal %d\n", signum); +} + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + int signals[] = { + SIGINT, + SIGQUIT, + SIGSEGV, + SIGTRAP, + 0 + }; + + if (_signals_block(signals) == -1) + { + fprintf(stderr, "Failed to block signals\n"); + return (EXIT_FAILURE); + } + + sleep(5); + if (handle_pending(&handler) == -1) + { + printf("Failed to set up handler for pending signals\n"); + return (EXIT_FAILURE); + } + + if (_signals_unblock(signals) == -1) + { + fprintf(stderr, "Failed to unblock signals\n"); + return (EXIT_FAILURE); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/2-main.c b/0x06-signals/tests/2-main.c new file mode 100644 index 0000000..acea131 --- /dev/null +++ b/0x06-signals/tests/2-main.c @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "signals.h" + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + int i; + + if (handle_sigaction() == -1) + { + printf("Failure\n"); + return (EXIT_FAILURE); + } + for (i = 0; ; i++) + { + printf("[%d] Wait for it ...\n", i); + sleep(1); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/3-main.c b/0x06-signals/tests/3-main.c new file mode 100644 index 0000000..85a9e94 --- /dev/null +++ b/0x06-signals/tests/3-main.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#include "signals.h" + +/* Our functions */ +void print_hello(int); +void set_print_hello(void); + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + void (*handler)(int); + int i; + + handler = current_handler_sigaction(); + printf("Address of the current handler: %#lx\n", (unsigned long int)handler); + + /* Set 'print_hello()` as the handler for SIGINT */ + set_print_hello(); + + handler = current_handler_sigaction(); + printf("Address of the 'print_hello' function: %#lx\n", (unsigned long int)&print_hello); + printf("Address of the current handler: %#lx\n", (unsigned long int)handler); + + for (i = 0; ; i++) + { + printf("[%d] Wait for it ...\n", i); + sleep(1); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/4-main.c b/0x06-signals/tests/4-main.c new file mode 100644 index 0000000..20d651a --- /dev/null +++ b/0x06-signals/tests/4-main.c @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "signals.h" + +/** + * main - Entry point + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + int i; + + if (trace_signal_sender() == -1) + { + printf("Failure\n"); + return (EXIT_FAILURE); + } + for (i = 0; ; i++) + { + printf("[%d] Wait for it ...\n", i); + sleep(1); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/7-main.c b/0x06-signals/tests/7-main.c new file mode 100644 index 0000000..c6c75b9 --- /dev/null +++ b/0x06-signals/tests/7-main.c @@ -0,0 +1,22 @@ +#include +#include +#include + +/** + * main - Simple program, printing its PID and running infinitely + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + pid_t pid; + + pid = getpid(); + printf("PID: %d\n", (int)pid); + while (98) + { + printf("Waiting ...\n"); + sleep(1); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/tests/8-main.c b/0x06-signals/tests/8-main.c new file mode 100644 index 0000000..c6c75b9 --- /dev/null +++ b/0x06-signals/tests/8-main.c @@ -0,0 +1,22 @@ +#include +#include +#include + +/** + * main - Simple program, printing its PID and running infinitely + * + * Return: EXIT_SUCCESS or EXIT_FAILURE + */ +int main(void) +{ + pid_t pid; + + pid = getpid(); + printf("PID: %d\n", (int)pid); + while (98) + { + printf("Waiting ...\n"); + sleep(1); + } + return (EXIT_SUCCESS); +} diff --git a/0x06-signals/9-main.sh b/0x06-signals/tests/9-main.sh old mode 100644 new mode 100755 similarity index 100% rename from 0x06-signals/9-main.sh rename to 0x06-signals/tests/9-main.sh diff --git a/README.md b/README.md index 4c1e465..92369a1 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,17 @@ Revisiting (235) 0x16. C - Simple Shell to build a second clone of `dash` / Ubun Project 0x02 found in separate repo [`shell_v2/`](https://github.com/allelomorph/shell_v2). -### [0x03. Python - /proc filesystem](./0x03-proc_filesystem/) +### [(360) 0x03. Python - /proc filesystem](./0x03-proc_filesystem/) +Introduction to using the `/proc` filesystem to explore the memory footprint of a running process. -### [0x04. C - ELF: readelf](./0x04-readelf/) +### [(364) 0x04. C - ELF: readelf](./0x04-readelf/) +Introduction to ELF or the Executable and Linkable Format, its uses, and structure. -### [0x05. x86 Assembly - libASM](./0x05-libasm/) +### [(376) 0x05. x86 Assembly - libASM](./0x05-libasm/) +Introduction to x86 assembly: Intel vs AT&T, flag registers, stack frames, function and system calls. -### [0x06. C - Signals](./0x06-signals/) +### [(362) 0x06. C - Signals](./0x06-signals/) +Introduction to Unix signals: sending, listening, and catching. ### [0x07. C - ELF: nm/objdump](./0x07-nm_objdump/) @@ -35,6 +39,8 @@ Project 0x02 found in separate repo [`shell_v2/`](https://github.com/allelomorph ### [0x0B. C - malloc](https://github.com/allelomorph/malloc) Project 0x0B found in separate repo [`malloc/`](https://github.com/allelomorph/malloc). +### [0x0C. Sockets](./0x0C-sockets/) + --- ## Author