From 7c701d1e7d6e637d0821b2a06944d8f56700739f Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 17 Nov 2021 22:43:30 +0500 Subject: [PATCH] Rewrite in Rust --- .gitignore | 4 +--- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 16 ++++++++++++++++ Makefile | 30 +++++++----------------------- config.mk | 15 --------------- main.c | 35 ----------------------------------- src/main.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ status.c | 44 -------------------------------------------- status.h | 9 --------- 9 files changed, 88 insertions(+), 129 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml delete mode 100644 main.c create mode 100644 src/main.rs delete mode 100644 status.c delete mode 100644 status.h diff --git a/.gitignore b/.gitignore index 6859f89..b83d222 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -*.o - -/polytree-session +/target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6998584 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "libc" +version = "0.2.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" + +[[package]] +name = "polytree-session" +version = "0.0.0" +dependencies = [ + "libc", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7074b26 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "polytree-session" +version = "0.0.0" +authors = ["Alex Kotov "] +edition = "2021" +description = "Polytree session manager" +readme = true +homepage = "https://github.com/PolytreeWM/polytree-session" +repository = "https://github.com/PolytreeWM/polytree-session.git" +license = "MIT" +keywords = ["gui"] +categories = ["gui"] +publish = true + +[dependencies] +libc = "0.2.107" diff --git a/Makefile b/Makefile index 7ab2849..a463bfb 100644 --- a/Makefile +++ b/Makefile @@ -1,33 +1,17 @@ # Polytree Session - session manager +all: target/debug/polytree-session + include config.mk -SRC = status.c main.c -HDR = status.h -OBJ = $(SRC:.c=.o) +SRC = Cargo.toml src/main.rs -all: options polytree-session - -options: - @echo Polytree Session build options: - @echo "CFLAGS = $(CFLAGS)" - @echo "LDFLAGS = $(LDFLAGS)" - @echo "CC = $(CC)" - -%.o: %.c - $(CC) -c $< -o $@ $(CFLAGS) - -OBJ: config.mk $(HDR) - -polytree-session: $(OBJ) - $(CC) -o $@ $(OBJ) $(LDFLAGS) - -clean: - rm -f polytree-session $(OBJ) +target/debug/polytree-session: $(SRC) + cargo build install: all mkdir -p $(DESTDIR)$(PREFIX)/bin - cp -f polytree-session $(DESTDIR)$(PREFIX)/bin + cp -f target/debug/polytree-session $(DESTDIR)$(PREFIX)/bin chmod 755 $(DESTDIR)$(PREFIX)/bin/polytree-session xinstall: install @@ -45,4 +29,4 @@ uninstall: $(DESTDIR)$(ICONSPREFIX)/polytree.png \ $(DESTDIR)$(XSESSIONSPREFIX)/polytree.desktop -.PHONY: all options clean install xinstall uninstall +.PHONY: all install xinstall uninstall diff --git a/config.mk b/config.mk index bad2ce3..d9781c9 100644 --- a/config.mk +++ b/config.mk @@ -6,18 +6,3 @@ SYSPREFIX = /usr ICONSPREFIX = $(SYSPREFIX)/share/icons XSESSIONSPREFIX = $(SYSPREFIX)/share/xsessions - -X11INC = /usr/X11R6/include -X11LIB = /usr/X11R6/lib -# FreeBSD (uncomment) -#X11INC = /usr/local/include -#X11LIB = /usr/local/lib - -INCS = -I${X11INC} -LIBS = -L${X11LIB} -lX11 -lpthread - -CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" -CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os $(INCS) $(CPPFLAGS) -LDFLAGS = $(LIBS) - -CC = cc diff --git a/main.c b/main.c deleted file mode 100644 index ae4db95..0000000 --- a/main.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "status.h" - -#include -#include -#include -#include -#include -#include -#include - -int main() -{ - const pid_t wm_pid = fork(); - - if (wm_pid == -1) { - perror("polytree-session: WM fork"); - exit(EXIT_FAILURE); - } - - if (wm_pid == 0) { - char *const args[] = { "polytreewm", NULL }; - execvp(args[0], args); - perror("polytree-session: WM exec"); - exit(EXIT_FAILURE); - } - - status_start(); - - int wm_status; - waitpid(wm_pid, &wm_status, 0); - - status_stop(); - - exit(WEXITSTATUS(wm_status)); -} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0e261e3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,48 @@ +use std::ffi::CString; +use std::thread; + +fn main() { + unsafe { unsafe_main() } +} + +unsafe fn unsafe_main() { + let wm_pid = libc::fork(); + + if wm_pid == -1 { + let msg = CString::new(b"polytree-session: WM fork" as &[u8]).unwrap(); + libc::perror(msg.as_ptr()); + libc::exit(libc::EXIT_FAILURE); + } + + if wm_pid == 0 { + let arg0 = CString::new(b"polytreewm" as &[u8]).unwrap(); + let args = vec![arg0.as_ptr(), std::ptr::null()]; + libc::execvp(arg0.as_ptr(), args.as_ptr()); + let msg = CString::new(b"polytree-session: WM exec" as &[u8]).unwrap(); + libc::perror(msg.as_ptr()); + libc::exit(libc::EXIT_FAILURE); + } + + let status_thread = thread::spawn(move || { + let pid = libc::fork(); + + if pid == -1 { return }; + + if pid == 0 { + let arg0 = CString::new(b"slstatus" as &[u8]).unwrap(); + let args = vec![arg0.as_ptr(), std::ptr::null()]; + libc::execvp(arg0.as_ptr(), args.as_ptr()); + let msg = CString::new(b"polytree-session: slstatus exec" as &[u8]) + .unwrap(); + libc::perror(msg.as_ptr()); + libc::exit(libc::EXIT_FAILURE); + } + }); + + let wm_status: i32 = 0; + libc::waitpid(wm_pid, wm_status as *mut i32, 0); + + status_thread.join().unwrap(); + + libc::exit(libc::WEXITSTATUS(wm_status)); +} diff --git a/status.c b/status.c deleted file mode 100644 index 22a814a..0000000 --- a/status.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "status.h" - -#include -#include -#include -#include -#include -#include - -static bool running = false; -static pid_t pid = 0; - -bool status_start() -{ - // TODO: maybe we should assert - if (running) return false; - running = true; - - pid = fork(); - - if (pid == -1) { - status_stop(); - return false; - } - - if (pid == 0) { - char *const args[] = { "slstatus", NULL }; - execvp(args[0], args); - perror("polytree-session: slstatus exec"); - exit(EXIT_FAILURE); - } - - return true; -} - -void status_stop() -{ - // TODO: maybe we should assert - if (!running) return; - running = false; - - kill(pid, SIGKILL); - waitpid(pid, NULL, 0); -} diff --git a/status.h b/status.h deleted file mode 100644 index 0ee6d09..0000000 --- a/status.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _STATUS_H -#define _STATUS_H - -#include - -bool status_start(); -void status_stop(); - -#endif // _STATUS_H