basic freebsd programming

cpghost at cpghost at
Sun Jan 2 11:45:12 PST 2005

On Sun, Jan 02, 2005 at 09:11:42PM +0300, Andrew P. wrote:
> The ones that are the most interesting for me now is how to
> write small daemons best and how to read ipfw info from a program.
> Of course I can refresh my C skills and gain some Unix-coding knowledge 
> by reading a couple' thousand pages, but I don't feel like it's 
> necessary for what I want to write - just a basic statistics collector.

Of course you could do that in C, but if all you need is a program
that reads the output of other programs, and presents stats on some
port as a daemon (and a client to read that out), why not just go for
a scripted solution in Perl or Python? Both languages are much better
than C when it comes to parsing strings, and they are very good at
networking too. IMHO, the only reason (besides efficiency) to write
a monitoring program in C is if you don't want (or can't afford) to
install a perl or python interpreter on the nodes that you want to

> Should I explore FreeBSD source code or is there some solid piece of 
> documentation?

That's not necessary. If you want to write that in C, you'll have
to familiarize yourself with the popen(3) call for executing a program
and capturing its output. Then you need a few string processing functions
like str*(3) sscanf() etc... to parse the output (that's the tricky part).
Finally you will need a small example of a client and server in C that
uses the sockets API (that's pretty generic and not FreeBSD-specific at
all, just google for it). Combine all this and voila, you've got your
nice monitoring app in C.

Alternatively, you could extract the info directly from the kernel
by performing exactly the same steps that your utility program (ipfw...)
does, but it's overkill for such a simple app.

But again, consider giving Python a try. It's well worth it for such
basic tasks.

> Best wishes,
> Andrew P.


Cordula's Web.

More information about the freebsd-questions mailing list