Skip to content

Instantly share code, notes, and snippets.

@viz3
Last active January 1, 2022 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viz3/a3035d36915395bf65667fbd3d20a909 to your computer and use it in GitHub Desktop.
Save viz3/a3035d36915395bf65667fbd3d20a909 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
close(pipefd[0]);
close(pipefd[1]);
exit(EXIT_FAILURE);
} else if (pid == 0) {
close(pipefd[0]);
dup2(pipefd[1], 2);
close(pipefd[1]);
execlp("dd", "dd", "if=/dev/zero", "of=dd.bin", "bs=1024M", "count=10", (char *)NULL);
perror("dd");
exit(EXIT_FAILURE);
} else {
close(pipefd[1]);
char buf[512];
ssize_t ret;
int fd = open("dd.log", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
while ((ret = read(pipefd[0], buf, sizeof(buf))) > 0) {
write(fd, buf, ret);
}
close(pipefd[0]);
close(fd);
int wstatus;
pid_t w = waitpid(pid, &wstatus, 0);
if (w < 0) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(wstatus)) {
int exit_code = WEXITSTATUS(wstatus);
printf("child exit code = %d\n", exit_code);
} else {
printf("child status = %04x\n", wstatus);
}
printf("parent process end\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
int wipe_disk(const char *name)
{
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
return 0;
}
pid_t cpid = fork();
if (cpid < 0) {
perror("fork");
close(pipefd[0]);
close(pipefd[1]);
return 0;
} else if (cpid == 0) {
close(pipefd[0]);
dup2(pipefd[1], 2);
close(pipefd[1]);
char of[64];
sprintf(of, "of=%s.bin", name);
char *const cmd[] = {"/usr/bin/dd", "if=/dev/zero", of, "bs=1024M", "count=1", NULL};
execv("/usr/bin/dd", cmd);
perror("execv");
return 0;
} else {
close(pipefd[1]);
char buf[512];
ssize_t r;
char log[64];
sprintf(log, "%s.log", name);
int fd = open(log, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
while ((r = read(pipefd[0], buf, sizeof(buf))) > 0) {
write(fd, buf, r);
}
close(pipefd[0]);
close(fd);
int wstatus;
pid_t w = waitpid(cpid, &wstatus, 0);
if (w < 0) {
perror("waitpid");
return 0;
}
if (WIFEXITED(wstatus)) {
int exit_code = WEXITSTATUS(wstatus);
printf("child exit code = %d\n", exit_code);
} else {
printf("child status = %04x\n", wstatus);
}
printf("parent process end\n");
}
return 1;
}
int main(int argc, char *argv[])
{
int count = 0;
char name[64];
for (int i = 'b'; i <= 'm'; i++) {
sprintf(name, "sd%c", i);
pid_t cpid = fork();
if (cpid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (cpid == 0) {
wipe_disk(name) ? exit(EXIT_SUCCESS) : exit(EXIT_FAILURE);
} else {
count++;
}
}
for (int i = 0; i < count; i++) {
int wstatus;
pid_t w = wait(&wstatus);
if (WIFEXITED(wstatus)) {
int exit_code = WEXITSTATUS(wstatus);
printf("child exit code = %d\n", exit_code);
} else {
printf("child status = %04x\n", wstatus);
}
printf("parent process end\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment