summaryrefslogtreecommitdiff
blob: a1bc1353e5733f5779b7b9893dcafa0160f35fed (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
;;; openrc.el --- OpenRC integration -*- lexical-binding: t -*-


;; Copyright 2022 Gentoo Authors


;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 2 of the License, or
;; (at your option) any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.


;; Author: Maciej Barć <xgqt@riseup.net>
;; Homepage: https://gitweb.gentoo.org/proj/emacs-openrc.git
;; Keywords: processes
;; Maintainer: <emacs@gentoo.org>
;; Package-Requires: ((emacs "24.3"))
;; Version: 0.0.0



;;; Commentary:


;; OpenRC integration.


;; This library was originally written as part of "emacs-gentoo"
;; by Maciej Barć.
;; It was later relicensed by the author under the GPL-2-or-later license
;; and republished under the Gentoo GNU Emacs project.

;; Original repository: https://gitlab.com/xgqt/emacs-gentoo



;;; Code:


;; Commands to consider:
;;  - rc-service -l  -- all of services
;;  - rc-status -f "ini" boot
;;  - rc-update show -v


(defconst openrc-version "0.0.0"
  "Emacs-Openrc version.")

(defgroup openrc nil
  "OpenRC integration."
  :group 'external)


;; Executables

(defcustom openrc-rc-command "/sbin/rc"
  "Path to the \"rc\" binary."
  :safe 'stringp
  :type 'file
  :group 'openrc)

(defcustom openrc-rc-update-command (concat openrc-rc-command "-update")
  "Path to the \"rc-update\" binary."
  :safe 'stringp
  :type 'file
  :group 'openrc)

(defcustom openrc-rc-service-command (concat openrc-rc-command "-service")
  "Path to the \"rc-service\" binary."
  :safe 'stringp
  :type 'file
  :group 'openrc)

(defcustom openrc-sudo-command "sudo"
  "Path to the \"sudo\" binary.
Used to gain privilege for some commands."
  :safe 'stringp
  :type 'file
  :group 'openrc)


;; Directories

(defcustom openrc-run-dir "/run/openrc"
  "Path to OpenRC run directory (defaults to \"/run/openrc\")."
  :safe 'stringp
  :type 'file
  :group 'openrc)

(defcustom openrc-started-dir (expand-file-name "started" openrc-run-dir)
  "Path to OpenRC directory containing started services."
  :safe 'stringp
  :type 'file
  :group 'openrc)


;; Other

(defcustom openrc-use-sudo
  (or (executable-find openrc-sudo-command)
      (file-executable-p openrc-sudo-command))
  "Whether to use \"sudo\" or \"su\" for commands that need root privileges.
The invoked \"sudo\" executable location is controlled by the
‘openrc-sudo-command’ variable."
  :type 'boolean
  :group 'openrc)



;; Helpers

(defun openrc--service-started? (service)
  "Check if SERVICE is started."
  (file-exists-p (expand-file-name service openrc-started-dir)))

(defun openrc--get-services ()
  "Get all OpenRC services."
  (mapcar
   (lambda (s)
     (let* ((lst (split-string s "|" t " *"))
            (service (car lst))
            (runlevel (cdr lst)))
       (vector service
               (if (openrc--service-started? service) "YES" "NO")
               (if (equal runlevel nil) "none" (car runlevel)))))
   (split-string (shell-command-to-string
                  (concat openrc-rc-update-command " show -v")) "\n" t)))

(defun openrc--tabulated-list (vectors list-length tabulated-list)
  "Create a TABULATED-LIST from list of VECTORS of length LIST-LENGTH."
  (cond
   ((equal vectors nil) tabulated-list)
   (t (openrc--tabulated-list
       (cdr vectors)
       list-length
       (append tabulated-list
               (list (list (- list-length (length vectors))
                           (car vectors))))))))

(defun openrc-refresh-services ()
  "Refresh the list of OpenRC services."
  (interactive)
  (message "Refreshing OpenRC services list...")
  (let ((services (openrc--get-services)))
    (setq tabulated-list-entries
          (openrc--tabulated-list services (length services) '())))
  (tabulated-list-init-header)
  (tabulated-list-print t)
  (message "...OpenRC services list refresh done!"))

(defun openrc--async-shell-command (privileged &rest args)
  "Run `async-shell-command' with ARGS.
If PRIVILEGED is true, then use the \"sudo\" or \"su\" to run the command."
  (let ((buffer-name "*OpenRC Command*")
        (error-buffer-name "*OpenRC Command ERRORS*")
        (command (apply 'concat (mapcar (lambda (s) (concat " " s)) args))))
    (let ((buffer (get-buffer-create buffer-name)))
      (with-current-buffer buffer
        (if privileged
            (if openrc-use-sudo
                (async-shell-command (concat "sudo " command)
                                     buffer-name
                                     error-buffer-name)
              (async-shell-command (concat "su -c \"" command "\"")
                                   buffer-name
                                   error-buffer-name))
          (async-shell-command command buffer-name error-buffer-name))
        (view-mode)))))


;; Describing services

(defun openrc--describe (service)
  "Describe a SERVICE."
  (openrc--async-shell-command
   nil openrc-rc-service-command service "describe" "--verbose"))

(defun openrc-describe-entry ()
  "Describe a service under the tabulated list entry."
  (interactive)
  (openrc--describe (aref (tabulated-list-get-entry) 0)))


;; Stopping and starting services

(defun openrc--toggle (service started?)
  "Start or stop a SERVICE depending on STARTED? state."
  (openrc--async-shell-command
   'privileged
   openrc-rc-service-command
   service
   (if started? "stop" "start") "--verbose"))

(defun openrc-toggle-entry ()
  "Start or stop a service under the tabulated list entry depending on state."
  (interactive)
  (let ((service (aref (tabulated-list-get-entry) 0)))
    (openrc--toggle service (openrc--service-started? service))))

(defun openrc--restart (service)
  "Restart a SERVICE."
  (openrc--async-shell-command
   'privileged openrc-rc-service-command service "restart" "--verbose"))

(defun openrc-restart-entry ()
  "Restart a service under the tabulated list entry."
  (interactive)
  (openrc--restart (aref (tabulated-list-get-entry) 0)))


;; Adding and removing services from runlevels

(defconst openrc--runlevels
  '("boot" "default" "nonetwork" "shutdown" "sysinit")
  "List of available runlevels.")

(defun openrc--read-runlevel ()
  "Read a runlevel selected by the user."
  (completing-read "Runlevel:" openrc--runlevels))

(defun openrc--add (service runlevel)
  "Add a SERVICE to a RUNLEVEL."
  (openrc--async-shell-command
   'privileged openrc-rc-update-command "add" service runlevel "--verbose" ))

(defun openrc-add-entry ()
  "Add a service under the tabulated list entry to a runlevel."
  (interactive)
  (openrc--add
   (aref (tabulated-list-get-entry) 0) (openrc--read-runlevel)))

(defun openrc--del (service runlevel)
  "Remove a SERVICE from a RUNLEVEL."
  (openrc--async-shell-command
   'privileged openrc-rc-update-command "del" service runlevel "--verbose"))

(defun openrc-del-entry ()
  "Remove a service under the tabulated list entry from a runlevel."
  (interactive)
  (openrc--del
   (aref (tabulated-list-get-entry) 0) (openrc--read-runlevel)))


;; Mode

(defvar openrc-services-menu-mode-hook nil
  "Hook for `openrc-services-menu' major mode.")

(defvar openrc-services-menu-mode-map
  (let ((openrc-services-menu-mode-map (make-keymap)))
    (define-key
      openrc-services-menu-mode-map (kbd "/") #'isearch-forward)
    (define-key openrc-services-menu-mode-map (kbd "e")
      #'openrc-describe-entry)
    (define-key openrc-services-menu-mode-map (kbd "a")
      #'openrc-add-entry)
    (define-key openrc-services-menu-mode-map (kbd "d")
      #'openrc-del-entry)
    (define-key openrc-services-menu-mode-map (kbd "g")
      #'openrc-refresh-services)
    (define-key openrc-services-menu-mode-map (kbd "r")
      #'openrc-restart-entry)
    (define-key openrc-services-menu-mode-map (kbd "t")
      #'openrc-toggle-entry)
    openrc-services-menu-mode-map)
  "Key map for `openrc-services-menu' major mode.")

(define-derived-mode openrc-services-menu-mode tabulated-list-mode
  "OpenRC Services Menu"
  "Major mode for listing the OpenRC services."
  (setq tabulated-list-format
        [("Service"  30 t)
         ("Started"  10 t)
         ("Runlevel" 20 t)])
  (setq tabulated-list-sort-key (cons "Runlevel" nil))
  (run-hooks 'openrc-services-menu-mode-hook)
  (use-local-map openrc-services-menu-mode-map))


;; Main provided features

;;;###autoload
(defun openrc-list-services ()
  "Display a list of OpenRC services."
  (interactive)
  (let ((buffer (get-buffer-create "*OpenRC Services*")))
    (with-current-buffer buffer
      (openrc-services-menu-mode)
      (openrc-refresh-services))
    (display-buffer buffer)))


(provide 'openrc)



;;; openrc.el ends here