Creating a symlink with autotools

I am working on some software that needs to run at startup time. The modified Unix-like system on which we deploy has a setup where everything in /etc/init.d gets run at startup.  Usually the scripts in /etc/init.d are not run from this directory at startup. Instead, a symbolic link to these programs are created in another directory that is run at start up. The name of this directory depends on how the machine is running, but for most network type things it is /etc/rc.d/rc3.d. The symlink in there starts with the letter S to show that it is supposed to run at startup, then followed by a number to signify the order. Yes, this is very like programming in BASIC. For instance crond, the process that is designed to run other processes on a schedule is started by /etc/rc.d/rc3.d/S90crond. Other network services are run from xinetd (extended internet daemon) at from /etc/rc.d/rc3.d/S56xinet, so they are available before scheduled tasks.

Time for me to return to the topic of this post. My program is installed in /bin. In order for it to be run at startup I need to put a symlink into /etc/init.d. Here’s the steps:

1. Modify configure.ac to know about the symlink program by adding a single line with the magic words:

AC_PROG_LN_S

To expand: autoconf program ln -s. This creates a scrpit segment to test that the program ln exists and the -s option creates a symlink. Since I also need the ensure my target directory is there I add:

AC_PROG_MKDIR_P

mkdir -p <path> creates all the directories specified by path that do not already exist. For instace:

mkdir /tmp/1/2/3/4/5/6/7/8/9

Will fail on most sytems if any of the direcories in /tmp/1/2/3/4/5/6/7/8 don’t exist. If all you have is /tmp,

mkdir -p /tmp/1/2/3/4/5/6/7/8/9

will create /tmp/1, then /tmp/1/2, and so on.

2. Modify the Makefile.ac to know about my program, which I will call watchdog.

install-exec-hook:

 $(MKDIR_P) $(DESTDIR)/etc/init.d

 $(LN_S) $(DESTDIR)/bin/watchdog $(DESTDIR)/etc/init.d

Because of the way this gets built I need to create the init.d directory. Note that this kind of modification can allow any general post-install scripting necessary. Since I am building in a subdirectory that later gets archived up, I have to use $(DESTDIR).If I didn’t add $(DESTDIR) it would try to do it on my local machine, and fail on a permissions check. If I was building as root, it would silently succeed and wreak havoc.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.