Reputation: 54242
I'm trying to use jsvc to make a daemon process, and it sounds like certain things can be done as root with it (for example, Tomcat can apparently bind to privileged ports). What I'm wondering is how to do that.
In my simple Daemon program, I try to open some files that are only readable as root during the init()
process, but I'm already running as the user I selected by then (in my case, "nobody"). If Tomcat can bind to privileged ports, it seems like I should be able to open root-owned config files.
Am I trying to do something that jsvc isn't meant for, or am I just missing something?
My code:
public class MediaProcessorDaemon implements Daemon {
ClassPathXmlApplicationContext spring = null;
/*- (non-Javadoc)
* @see org.apache.commons.daemon.Daemon#init(org.apache.commons.daemon.DaemonContext)
*/
@Override
public void init(DaemonContext context) throws DaemonInitException, Exception {
/* This next line throws an exception */
this.spring = new ClassPathXmlApplicationContext("/META-INF/spring/media-processor-context.xml");
}
/*- (non-Javadoc)
* @see org.apache.commons.daemon.Daemon#start()
*/
@Override
public void start() throws Exception {
this.spring.start();
}
/*- (non-Javadoc)
* @see org.apache.commons.daemon.Daemon#stop()
*/
@Override
public void stop() throws Exception {
if (this.spring != null) {
this.spring.stop();
}
}
/*- (non-Javadoc)
* @see org.apache.commons.daemon.Daemon#destroy()
*/
@Override
public void destroy() {
if (this.spring != null) {
this.spring.close();
}
}
}
And the error message:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: /etc/media/media-processor.properties (Permission denied)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.mycompany.mediaprocessor.MediaProcessorDaemon.init(MediaProcessorDaemon.java:24)
[snip]
So in init()
, I'm trying to open a file which is readable only by root (/etc/media/media-processor.properties), and I'm getting "Permission denied".
I execute it like this:
sudo jsvc -debug -user nobody -cp $classPath com.mycompany.MediaProcessorDaemon
Upvotes: 4
Views: 2458