Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

use_pty with non-tty stdin: command starts in background pgrp; foreground handoff only reaches the original pgrp, so a shell that creates its own pgrp (zsh) ends up unusable on FreeBSD/macOS

オープン
#583 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
c

調査の方向性

Start by tracing the use_pty execution and SIGCONT_FG handoff paths in sudo’s C source; the issue provides debug output and a FreeBSD truss observation, but no source files or tests. Reproduce with the posted command on FreeBSD or macOS, then compare the process group and terminal foreground group during the handoff. Done means zsh gets a usable controlling terminal with non-tty stdin while existing behavior remains intact.

索引モデルが issue の本文から書いたものです。

説明

use_pty with non-tty stdin: command starts in a background pgrp; the foreground handoff only reaches the original pgrp, so a shell that creates its own pgrp (zsh) is unusable on FreeBSD/macOS

Version / platforms
  • sudo 1.9.17p2 on FreeBSD 14 and 15 and macOS 27: fails
  • sudo 1.9.17p2 on Fedora: works (Ubuntu also works, with Defaults use_pty set)
  • zsh 5.9 / 5.9.2
Reproducer

With use_pty enabled, from an interactive terminal:

sudo sh -c 'cat; exec zsh </dev/tty' <<<ok
Actual result (FreeBSD/macOS)
ok
zsh: can't set tty pgrp: interrupt

The resulting zsh has no job control and does not execute commands (Enter does nothing). bash and ksh started the same way (exec bash </dev/tty) are fully usable.

Also on FreeBSD:

sudo sh -c 'cat; head -1 </dev/tty; echo "rc=$?"' <<<ok

returns immediately with rc=0 and no output. Under truss, head instead blocks and reads typed input, so there is a timing component.

Expected result

Same as Linux: the shell gets a usable controlling terminal.

Workarounds
  • Defaults>root !use_pty fixes it.
  • Adding kill -TTIN $$ before the exec fixes it, with job control working:
    sudo sh -c 'cat; kill -TTIN $$; exec zsh </dev/tty' <<<ok
    
Findings from Debug sudo /var/log/sudo_debug all@debug
stdin not user's tty, not logging
exec_pty: follower: 11, stdin: 14, stdout: 11, stderr: 11
exec_cmnd_pty: executing /bin/sh in the background
mon_handle_sigchld: command (16272) stopped, SIGTTIN
suspend_sudo_pty: command received SIGTTIN, parent running in the foreground
scheduled SIGCONT_FG for command
received SIGCONT_FG from parent

In truss on FreeBSD, head receives SIGTTIN from the kernel on its first read of /dev/tty. A SIGCONT from the sudo front-end follows, and then the read succeeds.

Analysis (my interpretation, not verified in the sudo source or the kernel)
  • With a non-tty stdin, sudo deliberately starts the command in a background process group on the pty. The foreground is granted only after the command stops on SIGTTIN/SIGTTOU, and it is granted to the group of the command sudo launched.
  • zsh at startup moves itself into a new process group and calls tcsetpgrp(). From the background this appears to raise SIGTTOU. The monitor's handoff then goes to the original group, not zsh's new one. The call fails with EINTR, and zsh stays in a background group with no input.
  • The kill -TTIN $$ workaround triggers the handoff while the shell is still in the original group. exec keeps the same pid and group, so zsh then finds itself in the foreground.
  • It is unclear why Linux works with the same sudo version. I have not investigated that.
Possible fix directions
  • Start the command in the foreground when the pty is its only terminal.
  • Or make the SIGCONT_FG handoff follow the stopped process's current process group instead of the command pid.
主要言語
C
スター
1.7k
フォーク
306
平均マージ
1日 2時間
マージ済み PR(30日)
22

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sudo-project/sudo のほかの issue

sudo-project/sudo の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。