Terminal, zsh & [Process Completed]

This is mostly a “me too!” post, but it’s been bugging me. Every other time I open a new Terminal window (under OS X Tiger, anyway), I get a [Process Completed] message instead of my shell. According to other people, this happens more if you use zsh, and especially if you close the window using ⌘-W. Interestingly, the same problem occurs in iTerm.

Well, I spent some time with ktrace, zsh, bash and Terminal. Sadly, the results aren’t terribly informative (so far).

First of all, I traced both bash and zsh exiting when ⌘-W was pressed. Neither was particularly interesting. There were no cleanups that one performed that the other did not.

Next, I traced Terminal (and its child processes) when starting up zsh. Twice. The first time everything worked, the second time everything broke. Now the trace makes the point of breakage plainly clear. The broken one gets stuck in a loop reading EOF from file descriptor 17, whereas the working one does not.

Looking back through the trace, file descriptor 17 is opened to /dev/ptyp4. According to pty, that’s a master pseudo terminal. It’s opened inside the Terminal process itself, which then forks, dup2’s the master pty to fd 0 (stdin) and then exec’s /usr/libexec/pt_chown (pt_chown source in FreeBSD should be similiar, judging by strings output).

We don’t see any output for pt_chown as it’s setuid root. But Terminal waits for it to finish, after it’s presumably corrected the ownership of the slave tty. Next, fd 18 is opened by Terminal as the slave tty4, Terminal forks and it’s then dup’d to stdin, stdout and stderr. fd 17 is then closed in that process and login is called.

At this point, Terminal’s got pty4 open, and login’s got tty4 and it’s basically a pipe between the two processes. Except that the kernel is making it look like a genuine 70’s era serial connection to the slave.

Anyway, it’s here that the two traces diverge (coming back to the original problem). The good session reads from the master pty, and then calls stat(2) on tty4. But it’s weird as the trace shows no return value for the read call. OTOH, the broken trace just shows a return value of EOF (0 bytes read) and loops around doing that for a few thousand times.

Funnily enough, the trace looks pretty much identical to that which would be produced by program 19.3 in Rich Steven’s APUE (best book I ever bought).

Sadly, it still hasn’t gotten me to the bottom of the problem. But I have refreshed my memory about how pseudo-terminals work. 🙂 I suspect that to make further progress, I’d have to run login under ktrace as well. I imagine that something is going wrong in there causing it to exit early, which is why the read in the parent returns EOF. Even then, I’m not sure if running it all as root would affect the outcome. Probably is my guess…

And no, I will not stop using zsh!

Update: A useful tip for debugging what your shell is doing at startup under osx. Add these lines to the top of ~/.zshrc (or ~/.bashrc if you’re a luddite 😉 ):

  lsof -p $$ > ~/lsof.out.$$
  ktrace -p $$ -f ~/ktrace.out.$$

This still doesn’t capture what’s going on in the parent process (login), but it does give you an idea of what the shell is getting up to.


A Month of Mac

It’s now been a month since my shiny new Mac arrived. Overall, I’m still really, really happy with it. I’ve bought NetNewsWire for feed reading and textmate for editing. I’m a little concerned that I don’t have the source code for my editor, but we’ll see how that goes.

I’m normally a devout Emacs or Vim user, but Aquamacs turned out to be just too different, despite what Tim Bray says about it. Carbon Vim was a bit better, but still felt a little clunky. TextMate just felt right, and after the recommendations from the rails people, it seemed like a good idea.

The main irritation that I’ve been having is with the keyboard. Apple keyboards don’t come easily to UK Unix users. The tilde is in the wrong position to start with (it’s been swapped with backslash), and I find myself continually hitting the wrong one. Worse is the fact that there’s no hash key on the keyboard in the UK layout. Oh all right, you can hit ⌥-3, but that pastes into the shell as UTF-8 meaning that you can’t comment things out properly in Vim. Why? I have no idea. But it’s meant that I’ve stuck to the US keyboard layout for now, which sucks.

By far and away the worst problem, though is the simple fact that you can’t copy CDs easily. The damned machine comes with a CD burner. It should be a piece of cake to say “take the bits off of this one, then right them on to this blank disk instead”. But no. The only option is to import into iTunes (converting into MP3) and then write the low-fi tracks out from iTunes again. With a 2-second gap between tracks. That really, badly sucks. Particularly when I found out that a previous version of OSX used to come with a “disk copy” utility. Thanks a bunch, Apple.


Shiny New Toy

I’ve been so busy with my shiny new toy recently, that I completely forgot to blog about it. I’ve recently acquired an iMac G5. I love it. I promised myself that I wouldn’t buy another PC because they’re generally rubbish, error prone and don’t come with much good software. The iMac comes with Emacs, Vim,
Perl, and Apache. What more could you ask for?

On top of that, the display on the iMac is superb. Lovely, bright and colourful. I’ve seen lots of macs around in the Perl community and consequently I’m pretty happy with the interface. The only slight downside so far is the odd keyboard layout, which has double-quote on the right hand side of the keyboard (american style) instead of on Shift-2 like every other british keyboard. But that’s minor.

When it comes to software, I happily downloaded everything I needed pretty quickly: graphical Carbon Vim, Aquamacs, Firefox and Thunderbird. Everything worked like a charm. And it’s all incredibly nippy compared to my old laptop.

However, I was particularly pleased to see that the copy of Myst IV Revelation also supported OSX. On the same CD. Bonus! And so did World of Warcraft when we bought that a few days later. Overall, the mac is an excellent gaming machine.

I’d definitely recommend getting one of these. With a bit of luck, they’ll only get cheaper as Apple release the new Intel products later on this year.