Linux fork detection using thread specific keyrings

Linux fork detection using thread specific keyrings

I was reading Linux’s keys.txt and realised that this provided a place to store process or even thread local data that didn’t depend on libc.

Detecting if the current process has been forked is important for PRNGs (Psuedo random number generators). Normally fork detection has been done by checking getpid() and/or the installation of an pthread_atfork handler, but this can be fooled due to pid wrapping (see here).

I wrote up this example to show how you might accomplish pid detection on Linux utilising the kernel’s thread specific keyring.

HOWTO: Generate a self-signed SSL cert in one command

Most guides out on the internet about how to generate a self-signed cert and long and complicated, but it can be done in a single command:

openssl req -newkey 2048 -keyout cert.key -nodes -x509 -out cert.pem -batch

The meaning of the options:

  • -newkey 2048: generate a new 2048 bit RSA private key instead of using an existing one
  • -keyout cert.key: save the rsa key in cert.key
  • -nodes: don’t encrypt the private key
  • -x509: generate an actual cert (rather than a certificate request)
  • -out cert.epm: save the certificate as cert.pem
  • -batch: don’t ask for the various SSL cert metadata fields

GPG Key

So today I finally got around to creating a GPG Key.

If you know me, please sign C91A9911192C187A

-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBFTET5IBEACrvuAFTakvetY6zm0dDWo+iEb0oPpe/bgW4aABrLJTukdMC6D3
h/APBlOGok5W6pv2StikWV+sNoGsktRt8OVOZW2bvqoGGDaxe/T/aqN5giMVzoIx
PCCftm+XNY8zMIZNQ/07lViTd7i7dKE6mLnOY3cE9SIkdcHOBX68IOLwVZuHoknJ
oNsxVhMn7IJhylcDuK5ZB5MoSHR8dAqDDl862a9EdCUdz7WFlIK6MXTx6350NKq8
i33pBd4aPpJrqg4hSL3EtP2SasmvZcsyo0hh3/8GREmN+TeN14OZScgDg4yoiXqp
iwJOjb+VeuWCL/7Ena7owTNMxX/d7CF8ivvYIMyfL1Bm5vpbWnc7CXEGQVe+Wt0U
yli9Do6+jsiKGOktHkP4W3luwUI9kOliM+REaIHqu5eMvRNEzbAoy9Y4pLkIME7W
Ug+kpZLXURBXSxuSgkK42tHBP1azuHEpRq+OVTJ/5+5m71iEkYbb/ry66iLlH64L
gDkeIi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MJqdbAwiu5uCGB9hiBwdzhrOrHqc9TLCn2T6V5F5QRh/6s
PEtooHIN+MzolBdXsPpST+sJRNGIBeg/IRzHx9rXx1zGF7b4mv56LbONiLpOmV3D
E4JgDu0f4b6iaBg/Eeoow75iDPt235/2
=TuFA
-----END PGP PUBLIC KEY BLOCK-----

Abandoned attempt at Lua bindings to rabbitmq-c

Abandoned attempt at Lua bindings to rabbitmq-c

I need a Lua library to communicate with rabbitmq, so after looking around and only finding luajit ffi based ones, and an nginx specific STOMP driver, I decided to create my own.

rabbitmq-c seems to be the most popular C library, so I spent about half the day getting some bindings working. But as I made slow progress, I realised that the most functions (e.g. amqp_login) provided by rabbitmq-c were blocking, with no async variants were available. This was a no-go, as I need to use talk to rabbit from within another server process that cannot be held up.

Instead of letting it go to a complete waste, I figured I might as well open source the attempt. At least it might be a starting point for the next person to come along.