Simple logger with STDOUT, Files and syslog support for C projects in Linux

In this little note I wanna describe simple logging module for C programs in Linux. I’m using this code for years in my projects. This module supports different type of output “targets” – files, syslog and stdout.

Continue reading “Simple logger with STDOUT, Files and syslog support for C projects in Linux”

Getting Linux routing table using netlink.

In the previous article we discussed monitoring of the network interfaces using netlink. Now it’s time to do something more complex and interesting.
Let’s discover how to get and print system routing table like a “ip route” command do.

Continue reading “Getting Linux routing table using netlink.”

Simple Linux character device driver.

Character device is a one of the simplest way to communicate with module in the Linux kernel.
This devices are presented as special files in a /dev directory and supports direct reading and writing of any data, byte by byte, like a stream. Actually most of the pseudo-devices in /dev is a character devices: serial ports, modems, sound and video adapters, keyboards, some custom I/O interfaces. User space programs can easily open, read, write and do custom control requests with such device files.
Here I describing how to write a simple Linux kernel module which can create one or multiple character device.

Continue reading “Simple Linux character device driver.”

Connecting HTU21D temperature/humidity sensor to the Raspberry PI using simple C i2c interface

Previously in my projects I’m always used well known DHT22 (AM2302) temperature/humidity sensors. But I found that this sensors is not very stable and subject to hungs. In my case this device is worked about two weeks and then stops responding untill power rebooted. This is absolutely unacceptable on some distant and autonomous devices. After some googling I found that I’m not alone and some peoples also expirienced such problem.
I’ve decided to replace this sensors to something more reliable and more accurate. My choice fell on HTU21D from the Measurement Specialties. HTU21D is a quite reliable and precise sensor, much newer than DHT and uses standard i2c bus instead own 1-wire protocol. I2C interface was a determinative and in this article I want to describe connection of this device to the Raspberry PI in details.

Continue reading “Connecting HTU21D temperature/humidity sensor to the Raspberry PI using simple C i2c interface”

Monitoring Linux networking state using netlink

Once in my work i needed to monitor all changes in the Linux networking subsystem: adding or deleting IP addresses, routes and so on.
Maybe the best way to do this is use socket-based netlink technology. Using netlink we can “subscribe” to some network-related notifications from the kernel, also it’s possible to send commands to the network stack and change routing table, interfaces configurations and packets filtering. For example, popular utilities like “iproute2” is also using netlink to do their job.
Easiest way to access netlink sockets from the userspace is to use libnetlink library which provides a bunch of macroses, defines and functions.
Worst part of this library and whole netlink technology is a lack of the good examples.
Good solution in this case is use iproute2 source code to discover things you interesting in. This article is also may be used as good startup point.

Continue reading “Monitoring Linux networking state using netlink”