help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Control of fan-speed on Lenovo Thinkpads


From: Jean Louis
Subject: Re: Control of fan-speed on Lenovo Thinkpads
Date: Wed, 31 Mar 2021 08:35:52 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Stefan Monnier <monnier@iro.umontreal.ca> [2021-03-31 04:24]:
> > You probably mean process-file? There is no file-process.
> 
> Indeed, sorry.
> 
> > There is practical burden to use call-process, and it is with &rest
> > ARGS, as those have to be given all separately.
> 
> There is no such extra burden.  Arguments always have to be given
> separately.  If you forget about that, you get bugs and associated
> security holes.  Executing a process is not the same as executing an
> `shell` command.  The first is a fairly simple and safe operation
> (assuming you trust the executable you're launching), whereas the second
> is tricky and risky unless you're extra careful to quote everything just
> right and you're sure the shell is really the one you expect, etc...

Yes, and I follow the advise.

This works well:

(shell-command "sudo su -c -- root -c \"echo level full-speed > 
/proc/acpi/ibm/fan\"")

While the argument after last "-c" has to be full line, which again
defeats the principle of being extra carefull and quoting everything
in this "sudo" example. Other commands are fine. Following line works.

(call-process "sudo" nil (get-buffer-create "*sudo*") t "su" "-c" "--" "root" 
"-c" "echo level auto > /proc/acpi/ibm/fan")

Thus based on that line, it become clear that "sudo" command that
shall be able to redirect output must be made in such way to defeat
the purpose of quoting and being careful.

Thus the following line works with the function below without
problems. I have now implemented your advise but probably not in the
spirit how it was meant. but I do not know how to add the command to
below call-process line without making macro and using eval.

(sudo "echo level full-speed > /proc/acpi/ibm/fan")

(defun sudo (command)
  "Execute COMMAND with system command `sudo'."
  (let ((not-remote (not (file-remote-p default-directory)))
        (sudo-buffer (get-buffer-create "*sudo*"))
        (current-buffer (current-buffer)))
    (switch-to-buffer sudo-buffer)
    (erase-buffer)
    (switch-to-buffer current-buffer)
    (if not-remote
        (let* ((sudo `(call-process "sudo" nil ,sudo-buffer t "su" "-c" "--" 
"root" "-c" ,command))
               (status (eval sudo))
               (status (if (= 0 status) "Success" status))
               (current-buffer (current-buffer))
               (output (progn
                         (switch-to-buffer sudo-buffer)
                         (buffer-string))))
          (switch-to-buffer current-buffer)
          (message "%sStatus: %s" output status))
      (message "This `sudo' does not work on remote directory: %s" 
default-directory))))


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns



reply via email to

[Prev in Thread] Current Thread [Next in Thread]