-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudo_explit.cpp
40 lines (37 loc) · 1.23 KB
/
sudo_explit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void executeSudo() {
const char *args[] = {"/usr/bin/sudo", NULL};
const char *env[] = {NULL};
pid_t pid = fork();
if (pid == 0) {
execve(args[0], const_cast<char *const *>(args), const_cast<char *const *>(env));
std::cerr << "Ошибка выполнения sudo" << std::endl;
exit(EXIT_FAILURE);
} else if (pid < 0) {
std::cerr << "Ошибка fork" << std::endl;
exit(EXIT_FAILURE);
} else {
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
std::cout << "Процесс завершился с кодом: " << WEXITSTATUS(status) << std::endl;
}
}
}
int main() {
std::cout << "Запуск команды sudo..." << std::endl;
executeSudo();
char* rbx = (char*)malloc(100);
if (rbx == nullptr) {
std::cerr << "Ошибка: не удалось выделить память." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Освобождение ресурсов для: " << static_cast<void*>(rbx) << std::endl;
free(rbx);
return EXIT_SUCCESS;
}