2

I need to reload Squid daemon, the solution is:

system("/etc/init.d/squid reload\n"); 

but I think that there must be a more efficient solution than using the "system" call, what C instruction should I use?

Thank you very much.

3
  • 3
    @Amber: subprocess.call() in C? Commented Mar 31, 2011 at 7:39
  • This sounds like a system administration kind of task and it's best done with a higher level "scripting" language rather than with C. Commented Mar 31, 2011 at 7:57
  • Oh, hey, I'm sleepy. :) I was expecting something involving daemon administration to be involving a scripting language (and since Python often has C modules involved, the C tag didn't really throw me). Commented Mar 31, 2011 at 8:12

2 Answers 2

3

The absolutely fastest way to have Squid reload its configuration files would be to send a SIGHUP signal to the daemon using kill(). This is what squid -k reconfigure does, which in turn is what /etc/init.d/squid reload is most probably doing.

The problem with this approach is that you have to somehow discover the process ID of the squid daemon in your C code. The PID is usually stored in a text file somewhere under /var/run (/var/run/squid.pid in my case), that you can read-in - that saves you the hassle of looking through the process table, but it is still somewhat of a mess.

Considering that /etc/init.d/squid may also be performing custom operations and that you are not bound to reload the daemon every second or so, I'd say that you should go with your current solution. If you don't care about the return status of the script, you could also use the common fork() and exec() approach, which is asynchronous and, therefore, faster from your application's point of view.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use fork and exec if you really need a faster solution, but since the squid init script has to be run, and Squid has to do the work, any optimizations of your C program will only give a really marginal improvement.

EDIT:

On the other hand (after having looked at the Squid manual), some daemons react to signals, and Squid seems to do so. For example, it re-read its configuration files if you send a HUP signal to it:

kill(process-id-of-the-squid-dameon, SIGHUP);

1 Comment

That's it, now I need to discover squid process ID. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.