aboutsummaryrefslogtreecommitdiffstats
path: root/bin/clipd
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2023-01-14 04:30:53 +0000
committerterminaldweller <thabogre@gmail.com>2023-01-14 04:30:53 +0000
commitc81052b70888eb18dca82e33444ebbd9910f5ebc (patch)
tree6b6eb892438a15f7b4849d7508d61d9491b809c3 /bin/clipd
parentupdate (diff)
downloadscripts-c81052b70888eb18dca82e33444ebbd9910f5ebc.tar.gz
scripts-c81052b70888eb18dca82e33444ebbd9910f5ebc.zip
update
Diffstat (limited to 'bin/clipd')
-rwxr-xr-xbin/clipd83
1 files changed, 75 insertions, 8 deletions
diff --git a/bin/clipd b/bin/clipd
index ae04207..6c6d40e 100755
--- a/bin/clipd
+++ b/bin/clipd
@@ -4,6 +4,15 @@
-- luarocks-5.3 install --local luaposix
-- luarocks-5.3 install --local argparse
-- cat .clip_history | dmenu -l 10 | xsel -ib
+local string = require("string")
+local signal = require("posix.signal")
+local argparse = require("argparse")
+local sys_stat = require("posix.sys.stat")
+local unistd_getuid = require("posix.unistd.getuid")
+local unistd_getgid = require("posix.unistd.getgid")
+local unistd_getpid = require("posix.unistd.getpid")
+local posix_syslog = require("posix.syslog")
+
local function default_luarocks_modules()
local luarocks_handle = io.popen("luarocks-5.3 path --bin")
local path_b = false
@@ -24,12 +33,6 @@ local function default_luarocks_modules()
end
default_luarocks_modules()
-local string = require("string")
-local signal = require("posix.signal")
-local argparse = require("argparse")
-
-signal.signal(signal.SIGINT, function(signum) os.exit(128 + signum) end)
-
local function sleep(n) os.execute("sleep " .. tonumber(n)) end
local function trim(s) return s:gsub("^%s+", ""):gsub("%s+$", "") end
@@ -38,6 +41,60 @@ parser:option("-s --hist_size", "history file size", 200)
parser:option("-f --hist_file", "history file location",
"/home/devi/.clip_history")
+local function log_to_syslog(log_str, log_priority)
+ posix_syslog.openlog("clipd",
+ posix_syslog.LOG_NDELAY | posix_syslog.LOG_PID,
+ posix_syslog.LOG_LOCAL0)
+ posix_syslog.syslog(log_priority, log_str)
+ posix_syslog.closelog()
+end
+
+local function check_clip_hist_perms(clip_hist)
+ local uid = unistd_getuid()
+ local gid = unistd_getgid()
+ for k, v in pairs(sys_stat.stat(clip_hist)) do
+ if k == "st_uid" then
+ if v ~= uid then
+ log_to_syslog(
+ "clipboard history file owned by uid other than the clipd uid",
+ posix_syslog.LOG_CRIT)
+ os.exit(1)
+ end
+ end
+ if k == "st_gid" then
+ if v ~= gid then
+ log_to_syslog(
+ "clipboard history file owned by gid other than the clipd gid",
+ posix_syslog.LOG_CRIT)
+ os.exit(1)
+ end
+ end
+ if k == "st_mode" then
+ if v & sys_stat.S_IRWXU ~= 0 then
+ log_to_syslog(
+ "file permissions are too open. they need to be 0600.",
+ posix_syslog.LOG_CRIT)
+ os.exit(1)
+ end
+ end
+ end
+end
+
+local function check_pid_file()
+ local f = sys_stat("/var/run/clipd.pid")
+ if f ~= nil then
+ log_to_syslog("clipd is already running", posix_syslog.LOG_CRIT)
+ os.exit(1)
+ end
+end
+
+local function write_pid_file()
+ local f = io.open("/var/run/clipd.pid")
+ f.write(unistd_getpid())
+end
+
+local function remove_pid_file() end
+
local function loop(clip_hist, clip_hist_size)
local clips_table = {}
local hist_current_count = 0
@@ -83,5 +140,15 @@ local function loop(clip_hist, clip_hist_size)
end
end
-local args = parser:parse()
-loop(args["hist_file"], args["hist_size"])
+local function main()
+ signal.signal(signal.SIGINT, function(signum) os.exit(128 + signum) end)
+ local args = parser:parse()
+ check_clip_hist_perms(args["hist_file"])
+ check_pid_file()
+ write_pid_file()
+ local status, err = pcall(loop(args["hist_file"], args["hist_size"]))
+ if ~status then log_to_syslog(err, posix_syslog.LOG_CRIT) end
+ remove_pid_file()
+end
+
+main()