[ Content | Sidebar ]

Archives for 2020

Yangpu Bridge

October 13th, 2020

I’ve wanted to walk to the Yangtze river for a while now, seeing as it’s only just a bit north of Shanghai. I tried last week but the attempt ended in failure. As I got to the edge of the city I found myself walking through an endless expanse of warehouses and docks, which was not a lot of fun. It also wasn’t obvious if I’d be able to see much once I finally got there so I gave up and went home.

Typical scenery

Before the industrial wasteland there was a nice footpath along the Huangpu river. I passed another impressive bridge, the Yangpu bridge. This one is the 38th longest cable-stayed bridge in the world. Shanghai really is a great place for bridge spotters.

Yangpu bridge


October 9th, 2020

Finally finished!

What with having to stay in a lot more recently I’ve taken to doing puzzles. This one took ages: most of the pieces are either just blue or black.

Reaching the most difficult part

Thames Town

October 4th, 2020

With China clamping down on imitating foreign architecture of late it seems like a good time to visit Shanghai’s “Thames Town”.

Obligatory red phone box

This isn’t the first time I’ve visited one of these curiosities: back in 2018 I visited Paris-lite in Hangzhou.

Thames Town is way out in the suburb of Songjiang. I decided to make the trip more interesting by walking there from Qixin Road subway station which is at the end of line 12.

The route was surprisingly rural in places. But there was always some apartment complex or tower block visible on the horizon.

Rice fields next to the road

Eventually I got to Songjiang which was once a self-contained town but now merged into the Shanghai suburbs.

Lake in Songjiang

It was getting a bit late by the time I finally reached Thames Town, which limited photo taking opportunities. It’s just on the other side of the lake in the picture above.

Tourist information board!

They’ve done a pretty good job of replicating a generic “English market town”. The houses looked pretty authentic too, although it was hard to get a good look as they all have tall fences around them. I guess the occupants must be pretty annoyed with the number of tourists.

Xcowsay 1.5.1

October 1st, 2020

Back in February I wrote about xcowsay 1.5 which had been updated with Gtk3 support. At the time I knew it would cause problems with non-compositing window managers but I wasn’t sure if this would affect anyone. Well, I received complaints. I always hate it when software updates break otherwise working systems so I’d like to apologise for this and it’s now fixed with xcowsay 1.5.1.

The problem was caused by Gtk3 removing the gtk_widget_shape_combine_mask function which xcowsay used for transparency around the cow and the bubble. In xcowsay 1.5 I simply added an alpha channel to the windows and the compositor will blend them with the desktop underneath. However without a compositor it results in ugly black squares like this:

To work around this xcowsay 1.5.1 grabs the pixels from the root window and uses that as the background to draw the cow and bubble onto. This works fine unless the windows underneath move while the cow is displayed, but I think in practice this is unlikely to cause a problem.

Download the new release here: xcowsay-1.5.1.tar.gz

This release is also signed with with my PGP key: xcowsay-1.5.1.tar.gz.asc

Xupu Bridge

September 30th, 2020

I’m taking advantage of the fine weather to go out for another walk. This time down the west bank of the Huangpu river.

After a while I came to the Xupu bridge, which carries Shanghai’s outer ring road. I’ve seen this bridge a few times before (last weekend, in fact) but never got close to it. I found to my excitement you can take a ferry right underneath it!

The bridge is the 48th tallest in the world. Although a quick scan down that lists reveals the majority are also in China.

On the east bank there’s a small park with some rather excellent views of the ramp up onto the bridge. Reminds me a bit of the a Humber bridge country park I visited many years ago.

After that I walked to the Xinzhuang interchange, an enormous elevated highway intersection that looks amazing on the map but sadly the only pedestrian access is via a tunnel underneath.

Dianpu River

September 26th, 2020

The weather here is starting to cool off and with little sign of the coronavirus it’s time to venture out walking again. This time I tried to walk along the Dianpu river, a small river south of where I live, to the Huangpu river, the main river that runs through the centre of Shanghai.

Frustratingly there isn’t a continuous path running the whole length so eventually I settled for walking along nearby streets and crossing back over when there was a bridge.

Where the Dianpu river joins the larger Huangpu river

I thought there might be a ferry to the other side where I could continue walking. But although the terminal was marked on the map it was either permanently closed or still under construction.

SIGPIPE and how to ignore it

September 23rd, 2020

I recently found myself trying to port a program that uses Boost Asio to run on OpenBSD. Everything compiled OK but while running it would occasionally exit with an unhandled SIGPIPE signal. This doesn’t happen on Linux. What’s going on here?

SIGPIPE is a synchronous signal that’s sent to a process (thread in POSIX.1-2004) which attempts to write data to a socket or pipe that has been closed by the reading end. Importantly it’s not an asynchronous signal that notifies you when the reading end has been closed: it’s delivered only when you attempt to write data. In fact it’s generated precisely when the system call (write(2), sendmsg(2), etc.) would fail with EPIPE and doesn’t give any additional information.

So what’s the point then? The default action for SIGPIPE is to terminate the process without a core dump (just like SIGINT or SIGTERM). This simplifies error handling in programs that are meant to run as part of a shell pipeline: reading input, transforming it, and then writing it to another process. SIGPIPE allows the program to skip error handling and blindly write data until it’s killed.

For programs that handle write errors it doesn’t seem to be useful and is best avoided. But unfortunately there are several different ways to do that.

Ignore the signal globally

This is the easiest if you are in complete control of the program (i.e. not writing a library). Just set the signal to SIG_IGN and forget about it.



If you are writing to a socket, and not an actual pipe, pass the MSG_NOSIGNAL flag to send(2) or sendmsg(2). This has been in Linux for ages and was standardised in POSIX.1-2008 so it’s available almost anywhere.

Set SO_NOSIGPIPE socket option

This is a bit niche as it only exists on FreeBSD and OS X. Use setsockopt(2) to set this option on a socket and all subsequent send(2) calls will behave as if MSG_NOSIGNAL was set.

int on = 1;
setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on))

This seems to be of limited utility as calling write(2) on the socket will still generate SIGPIPE. The only use I can think of is if you need to pass the socket to a library or some other code you don’t control.

Temporarily mask the signal on the current thread

The most general solution, for when you are not in full control of the program’s signal handling and want to write data to an actual pipe or use write(2) on a socket, is to first mask the signal for the current thread with pthread_sigmask(3), write the data, drain any pending signal with sigtimedwait(2) and a zero timeout, and then finally unmask SIGPIPE. This technique is described in more detail here. Note that some systems such as OpenBSD do not have sigtimedwait(2) in which case you need to use sigpending(2) to check for pending signals and then call the blocking sigwait(2).

Anyway back to the original problem. Asio hides SIGPIPE from the programmer by either setting the SO_NOSIGPIPE socket option on systems that support it, or on Linux by passing MSG_NOSIGNAL to sendmsg(2). None of these apply to OpenBSD which is why we get the SIGPIPE. I submitted a pull request to pass MSG_NOSIGNAL on OpenBSD as well. But I don’t know when or if that will be merged so I’m also trying to get the same fix added to the ports tree.

OpenSMTPD: use SSL client certificate when relaying outgoing mail

September 13th, 2020

I recently set up OpenSMTPD as the MTA on my local machine. I want to relay outgoing mail through another mail server on my VPS which is configured to only accept SSL connections with valid client certificates.

It’s not clear from the documentation how to configure this in smtpd.conf. However I eventually found from the source code that the “relay” action accepts a “pki” option to specify a certificate and key file.

action "outbound" relay host smtps://user@mail.example.org \
	auth <secrets> pki host.example.org mail-from "@example.org"

My mail server requires a username and password in addition to the client certificate so a “secrets” table should also be configured:

table secrets file:/etc/mail/secrets

And finally add a “pki” stanza for host.example.org to associate the X.509 certificate and private key:

pki host.example.org cert "/etc/ssl/example.crt"
pki host.example.org key "/etc/ssl/private/example.key"

UPDATE: this is documented in the man page now. 😀

NVC Version 1.5

July 19th, 2020

I’m pleased to announce a new version of nvc, the VHDL simulator I’ve been working on for the best part of a decade now. I haven’t added any major new features recently but I’ve fixed several bugs listed below, and also updated the code generation to be compatible with LLVM 7 and later. Due to a licensing change the IEEE standard library sources can now be redistributed, but note that distributing modifications is not permitted so these are truly free software.

  • IEEE library sources are now distributed
  • Updated FST library to match GtkWave 3.3.79
  • The LXT wave output format is deprecated, use FST instead
  • Fix incorrect file name in assertion message
  • Fix crash while recovering from parse error
  • Add --dump-json command to print AST as JSON (from Sebastien Van Cauwenberghe)
  • Fix crash when using LLVM 7 and later
  • Fix spurious assertion failure in std.textio.readline
  • Reals are now rounded to the nearest integer as specified by the LRM
  • Fix crash when constant folding uses too much memory
  • Improved memory management in evaluator (thanks to Frank Mori Hess)
  • Various other minor fixes and improvements

Download the source package here: nvc-1.5.tar.gz.

My first Linux “kernel” patches

June 7th, 2020

OK well not really kernel patches, but they’re in the Linux tree so I guess it counts?

Was so excited when I got the automatic notification they’d been merged for the 5.8 release. Hopefully someone out there using perf to profile Java finds them useful.