Function: tramp-androidsu-maybe-open-connection

tramp-androidsu-maybe-open-connection is a byte-compiled function defined in tramp-androidsu.el.gz.

Signature

(tramp-androidsu-maybe-open-connection VEC)

Documentation

Open a connection VEC if not already open.

Mostly identical to tramp-adb-maybe-open-connection, but also disables multibyte mode and waits for the shell prompt to appear.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp-androidsu.el.gz
(defvar android-use-exec-loader) ; androidfns.c.

(defun tramp-androidsu-maybe-open-connection (vec)
  "Open a connection VEC if not already open.
Mostly identical to `tramp-adb-maybe-open-connection', but also disables
multibyte mode and waits for the shell prompt to appear."
  ;; During completion, don't reopen a new connection.
  (unless (tramp-connectable-p vec)
    (throw 'non-essential 'non-essential))

  (with-tramp-debug-message vec "Opening connection"
    (let ((p (tramp-get-connection-process vec))
	  (process-name (tramp-get-connection-property vec " process-name"))
	  (process-environment (copy-sequence process-environment)))
      ;; Open a new connection.
      (condition-case err
	  (unless (process-live-p p)
	    (with-tramp-progress-reporter
		vec 3
		(format "Opening connection%s for %s%s using %s"
			(if (tramp-string-empty-or-nil-p process-name)
			    "" (concat " " process-name))
			(if (tramp-string-empty-or-nil-p
			     (tramp-file-name-user vec))
			    "" (concat (tramp-file-name-user vec) "@"))
			(tramp-file-name-host vec)
			(tramp-file-name-method vec))
              (let* ((coding-system-for-read 'utf-8-unix)
                     (process-connection-type tramp-process-connection-type)
                     ;; The executable loader cannot execute setuid
                     ;; binaries, such as su.
                     (android-use-exec-loader nil)
		     (p (tramp-start-process
			 vec (tramp-get-connection-name vec)
			 (tramp-get-connection-buffer vec)
			 ;; Disregard `tramp-encoding-shell', as
			 ;; there's no guarantee that it's possible to
			 ;; execute it with `android-use-exec-loader'
			 ;; off.
			 tramp-androidsu-local-shell-name "-i"))
		     (user (tramp-file-name-user vec))
                     su-binary path command)
                ;; Set sentinel.
                (set-process-sentinel p #'tramp-process-sentinel)
                ;; Replace `login-args' place holders.  `PATH' must be
                ;; set to `tramp-androidsu-remote-path', as some `su'
                ;; implementations propagate their callers' environments
                ;; to the root session, which might be contaminated with
                ;; incompatible `ls' binaries or similar.
		(setq path (tramp-shell-quote-argument
                            (string-join tramp-androidsu-remote-path ":"))
                      su-binary
                      (shell-quote-argument
                       (or (executable-find "su")
                           (tramp-user-error
			    vec
                            "No su binary is available in any of `exec-path'")))
                      command (format "PATH=%s exec %s - %s || exit"
                                      path su-binary user))
                ;; Attempt to execute the shell inside the global mount
                ;; namespace if requested.
                (when tramp-androidsu-mount-global-namespace
                  (progn
                    (when (eq tramp-androidsu-su-mm-supported 'unknown)
                      ;; Change the prompt in advance so that
                      ;; `tramp-adb-send-command-and-check' can call
                      ;; `tramp-search-regexp'.
                      (tramp-adb-send-command
		       vec (format "PS1=%s PS2=''"
                                   (tramp-shell-quote-argument
                                    tramp-end-of-output)))
                      (setq tramp-androidsu-su-mm-supported
                            ;; Detect support for `su -mm'.
                            (tramp-adb-send-command-and-check
                             vec (format "%s -mm -c 'exit 24'" su-binary)
                             24)))
                    (when tramp-androidsu-su-mm-supported
                      (tramp-set-connection-property
                       vec "remote-namespace" t)
		      (setq command (format "PATH=%s exec %s -mm - %s || exit"
                                            path su-binary user)))))
                ;; Send the command.
		(tramp-message vec 3 "Sending command `%s'" command)
		(tramp-adb-send-command vec command t t)
		;; Android su binaries contact a background service to
		;; obtain authentication; during this process, input
		;; received is discarded, so input cannot be
		;; guaranteed to reach the root shell until its prompt
		;; is displayed.
		(with-current-buffer (process-buffer p)
		  (tramp-wait-for-regexp p tramp-connection-timeout
					 "#[[:space:]]*$"))
                ;; Set connection-local variables.
                (tramp-set-connection-local-variables vec)
                ;; Change prompt.
                (tramp-adb-send-command
		 vec (format "PS1=%s PS2=''"
			     (tramp-shell-quote-argument tramp-end-of-output)))
                ;; Disable line editing.
                (tramp-adb-send-command
                 vec "set +o vi +o vi-esccomplete +o vi-tabcomplete +o emacs")
                ;; Disable Unicode, for otherwise Unicode filenames will
                ;; not be decoded correctly.
                (tramp-adb-send-command vec "set +U")
                ;; Dump option settings in the traces.
                (when (>= tramp-verbose 9)
		  (tramp-adb-send-command vec "set -o"))
		;; Disable echo expansion.
		(tramp-adb-send-command
		 vec "stty -inlcr -onlcr -echo kill '^U' erase '^H'" t)
		;; Check whether the echo has really been disabled.
		;; Some implementations, like busybox, don't support
		;; disabling.
		(tramp-adb-send-command vec "echo foo" t)
		(with-current-buffer (process-buffer p)
		  (goto-char (point-min))
		  (when (looking-at-p "echo foo")
		    (tramp-set-connection-property p "remote-echo" t)
		    (tramp-message vec 5 "Remote echo still on. Ok.")
		    ;; Make sure backspaces and their echo are enabled
		    ;; and no line width magic interferes with them.
		    (tramp-adb-send-command
		     vec "stty icanon erase ^H cols 32767" t)))
		;; Mark it as connected.
		(tramp-set-connection-property p "connected" t))))
	;; Cleanup, and propagate the signal.
	((error quit)
	 (tramp-cleanup-connection vec t)
	 (signal (car err) (cdr err)))))))