C in sh

I am currently reading the best book about the C language since the K&R. It’s called 21st Century C, was written by Ben Klemens and published by O’Reilly.

There is one gem per page in this book. From big-picture tips to nice little hacks. My favority so far can be found on page 29. It explains how one can embed a small C proram into a shell script.

In a nutshell, this is how you do it:

#!/bin/sh

gcc -xc - << END_OF_C_SECTION
#include <stdio.h>
int main() {
   printf("C inside a shell script!\n");
}
END_OF_C_SECTION
./a.out

‘-xc’ indicates to cc that what follows in C code. Also notice the dash (‘-‘) that tells gcc to accept standard input instead of a file.

In a footnote, Klemens points to a complete implementation of this idea by RhysU, who has put his work on github.

Busybox, compile one applet only

It might be handy to compile busybox with only one applet, or a very minimal set of applets to minimise the binary size. This could be achieved by running “make menuconfig” and selecting / deselecting what is or is not wished, but if all you want is one or two applets, as so much is preselected by default, this is very tedious to do.

It would be great if there was a way to start clean, on a config file where nothing is preselected.

Well the way to that is to run:

make allnoconfig

Then run:

make menuconfig

and choose what you want. After the usual:

make -j 4

if you run the busybox binary, you will see listed only the applets you selected. If you selected only one applet though, it will be launch directly.

Neat!

pfSense notifications with Sendgrid

To have your pfSense device send you email notifications, you can configure it like so:

1. Create yourself a Sendgrid account on sendgrid.com

2. In pfSense (I currently use version 2.2.6), go to:

System > Advanced > Notifications (tab)

3. Set up the ‘SMTP E-Mail’ section as follows:

  • E-Mail server: smtp.sendgrid.net
  • SMTP Port of E-Mail server: 465
  • Secure SMTP Connection: check the box entitled: ‘Enable SMTP over SSL/TLS’
  • Put whatever you want and need in the field ‘From e-mail address’
  • Notification E-Mail address: This is the address the notifications emails should be sent to
  • Your Sendgrid credentials are to go to the fields: ‘Notification E-Mail auth username (optional)’ and ‘Notification E-Mail auth password’
  • Notification E-Mail auth mechanism: PLAIN

You can test by pressing the button ‘Test SMTP’ (duh!) and you can see the completion in the logs (Status > System Logs).