Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/.gitignore b/.gitignore
index 582fa80..c84c611 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
target
.maid
-tests
\ No newline at end of file
+tests
+bin
\ No newline at end of file
diff --git a/Maidfile.toml b/Maidfile.toml
index 8b9edb4..88c0a69 100644
--- a/Maidfile.toml
+++ b/Maidfile.toml
@@ -1,3 +1,15 @@
[project]
name = "pmc"
-version = "0.1.0"
\ No newline at end of file
+version = "0.1.0"
+
+
+[tasks]
+run = { script = ["maid build -q", "./bin/pmc"] }
+clean = { script = ["rm -rf bin", "mkdir bin"] }
+
+[tasks.build]
+depends = ["clean"]
+script = [
+ "cargo zigbuild --release",
+ "cp target/release/pmc bin/pmc"
+]
diff --git a/src/cmd.cc b/src/cmd.cc
index b63b09b..474bd4c 100644
--- a/src/cmd.cc
+++ b/src/cmd.cc
@@ -1,58 +1,64 @@
#include "include/cmd.h"
#include <fcntl.h>
#include <unistd.h>
+#include <stdio.h>
+#include <signal.h>
namespace cmd {
void Runner::New(const std::string &name, const std::string &logPath) {
std::string stdoutFileName = logPath + "/" + name + "-stdout.log";
std::string stderrFileName = logPath + "/" + name + "-stderr.log";
stdout_fd = open(stdoutFileName.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0644);
stderr_fd = open(stderrFileName.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0644);
}
Runner::~Runner() {
if (stdout_fd != -1) {
close(stdout_fd);
}
if (stderr_fd != -1) {
close(stderr_fd);
}
}
uint64_t Runner::Run(const std::string &command) {
pid_t pid = fork();
if (pid == -1) {
std::cerr << "Error: Unable to fork.\n";
return -1;
} else if (pid == 0) {
setsid();
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
dup2(stdout_fd, STDOUT_FILENO);
dup2(stderr_fd, STDERR_FILENO);
if (execl("/bin/bash", "bash", "-c", command.c_str(), (char *)nullptr) == -1) {
std::cerr << "Error: Unable to execute the command.\n";
exit(EXIT_FAILURE);
}
} else {
return pid;
}
close(stdout_fd);
close(stderr_fd);
return -1;
}}
uint64_t run_command(Str name, Str log_path, Str command) {
cmd::Runner runner;
runner.New(std::string(name), std::string(log_path));
return runner.Run(std::string(command));
}
+
+uint64_t kill_pid(uint64_t pid) {
+ return kill(pid, SIGTERM);
+}
diff --git a/src/include/cmd.h b/src/include/cmd.h
index 65f3a22..3333373 100644
--- a/src/include/cmd.h
+++ b/src/include/cmd.h
@@ -1,30 +1,31 @@
#ifndef cmd
#define cmd
#include "cxx.h"
#include <fstream>
#include <string>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace rust;
namespace cmd {
class Runner {
public:
void New(const std::string &name, const std::string &logPath);
uint64_t Run(const std::string &command);
~Runner();
private:
int stdout_fd;
int stderr_fd;
};
}
uint64_t run_command(Str name, Str log_path, Str command);
+uint64_t kill_pid(uint64_t pid);
#endif
diff --git a/src/main.rs b/src/main.rs
index 862fc52..013c8b4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,16 +1,21 @@
+use std::{thread, time::Duration};
+
#[cxx::bridge]
mod cmd {
unsafe extern "C++" {
include!("pmc/src/include/cmd.h");
fn run_command(name: &str, log_path: &str, command: &str) -> u64;
+ fn kill_pid(pid: u64) -> u64;
}
}
fn main() {
let name = "example";
let log_path = "tests/logs";
let command = "node tests/index.js";
let pid = cmd::run_command(&name, &log_path, &command);
println!("pid: {pid}");
+ thread::sleep(Duration::from_millis(1000));
+ cmd::kill_pid(pid);
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Feb 1, 3:55 PM (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
494841
Default Alt Text
(3 KB)

Event Timeline