Skip to content

Commit

Permalink
s r d wait command exit; support openwrt
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Jan 3, 2025
1 parent a7d57dc commit 5b7753a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 39 deletions.
78 changes: 49 additions & 29 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn _main() !void {
\\
\\ stop stop z daemon
\\
\\v20240927 https://github.com/txthinking/z
\\v20250103 https://github.com/txthinking/z
\\
\\
;
Expand All @@ -87,38 +87,53 @@ pub fn _main() !void {
return error.RootRequired;
}

const r = crontabL(allocator) catch null;
defer {
if (r) |r1| {
allocator.free(r1.b);
if (std.fs.cwd().access("/etc/openwrt_release", .{})) |_| {
const s =
\\
\\On OpenWrt:
\\ - z works file
\\ - z will not start automatically on boot, you may need to add a line to /etc/rc.local: /path/to/z start
\\ - seeing this message generally means that z has been started
\\ - you may also need: z e HOME $HOME
\\ - you may also need: z e PATH $PATH
\\
\\
;
try stdout.print(s, .{});
} else |_| {
const r = crontabL(allocator) catch null;
defer {
if (r) |r1| {
allocator.free(r1.b);
}
}
}
var cp = std.process.Child.init(&.{"crontab"}, allocator);
cp.stdin_behavior = .Pipe;
try cp.spawn();
if (r) |r1| {
var it = std.mem.splitScalar(u8, r1.b[0..r1.n], '\n');
while (it.next()) |v| {
if (v.len == 0 or std.mem.containsAtLeast(u8, v, 1, "z start")) {
continue;
var cp = std.process.Child.init(&.{"crontab"}, allocator);
cp.stdin_behavior = .Pipe;
try cp.spawn();
if (r) |r1| {
var it = std.mem.splitScalar(u8, r1.b[0..r1.n], '\n');
while (it.next()) |v| {
if (v.len == 0 or std.mem.containsAtLeast(u8, v, 1, "z start")) {
continue;
}
errdefer _ = cp.wait() catch .Unknown;
try cp.stdin.?.writeAll(v);
try cp.stdin.?.writeAll("\n");
}
}
{
errdefer _ = cp.wait() catch .Unknown;
try cp.stdin.?.writeAll(v);
try cp.stdin.?.writeAll("\n");
try cp.stdin.?.writeAll("@reboot ");
const bz = try allocator.alloc(u8, 4 * 1024);
defer allocator.free(bz);
const bz1 = try std.fs.selfExePath(bz);
try cp.stdin.?.writeAll(bz1);
try cp.stdin.?.writeAll(" start\n");
cp.stdin.?.close();
cp.stdin = null;
}
_ = try cp.wait();
}
{
errdefer _ = cp.wait() catch .Unknown;
try cp.stdin.?.writeAll("@reboot ");
const bz = try allocator.alloc(u8, 4 * 1024);
defer allocator.free(bz);
const bz1 = try std.fs.selfExePath(bz);
try cp.stdin.?.writeAll(bz1);
try cp.stdin.?.writeAll(" start\n");
cp.stdin.?.close();
cp.stdin = null;
}
_ = try cp.wait();

const b = try allocator.alloc(u8, 8);
defer allocator.free(b);
Expand Down Expand Up @@ -204,10 +219,15 @@ fn crontabL(allocator: std.mem.Allocator) !?struct { b: []u8, n: usize } {
}

test "simple test" {
// std.testing.refAllDecls(@This());
std.testing.refAllDecls(@This());
// var gpa = std.heap.GeneralPurposeAllocator(.{}){};
// defer {
// _ = gpa.deinit();
// }
// const allocator = gpa.allocator();
if (std.fs.cwd().access("/etc/openwrt_release", .{})) |_| {
std.debug.print("hello, a.\n", .{});
} else |_| {
std.debug.print("hello, b.\n", .{});
}
}
34 changes: 24 additions & 10 deletions src/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ pub fn init(allocator: std.mem.Allocator) !Server {
}

pub fn deinit(self: *Server) void {
var wait = false;
self.mutex.lock();
if (self.processes.items.len > 0) {
for (self.processes.items) |v| {
_ = v.process.kill() catch |err| {
self.e(err);
};
while (true) {
if (v.process.term != null) {
break;
}
std.time.sleep(1 * std.time.ns_per_s);
}
}
wait = true;
}
self.mutex.unlock();
if (wait) {
// wait for sub threads exit first, better communication way should probably be used
std.time.sleep(3 * std.time.ns_per_s);
}
self.mutex.lock();
for (self.commands.items) |v| {
for (v.args) |vv| {
Expand Down Expand Up @@ -227,6 +227,12 @@ fn _handle(self: *Server, conn: std.net.Server.Connection) !void {
for (self.processes.items) |v| {
if (v.id == id) {
_ = try v.process.kill();
while (true) {
if (v.process.term != null) {
break;
}
std.time.sleep(1 * std.time.ns_per_s);
}
}
}
}
Expand All @@ -241,12 +247,15 @@ fn _handle(self: *Server, conn: std.net.Server.Connection) !void {
for (self.processes.items) |v| {
if (v.id == id) {
_ = try v.process.kill();
while (true) {
if (v.process.term != null) {
break;
}
std.time.sleep(1 * std.time.ns_per_s);
}
}
}
}
self.mutex.unlock();
std.time.sleep(3 * std.time.ns_per_s);
self.mutex.lock();
for (p.value[1..]) |v0| {
errdefer self.mutex.unlock();
const id = try std.fmt.parseInt(i64, v0, 10);
Expand All @@ -273,6 +282,12 @@ fn _handle(self: *Server, conn: std.net.Server.Connection) !void {
for (self.processes.items) |v| {
if (v.id == id) {
_ = try v.process.kill();
while (true) {
if (v.process.term != null) {
break;
}
std.time.sleep(1 * std.time.ns_per_s);
}
}
}
for (self.commands.items, 0..) |v, i| {
Expand All @@ -286,7 +301,6 @@ fn _handle(self: *Server, conn: std.net.Server.Connection) !void {
}
}
}
std.time.sleep(3 * std.time.ns_per_s);
try self.saveCommands();
try self.endHandle(conn);
return;
Expand Down

0 comments on commit 5b7753a

Please sign in to comment.