ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You could make the daemon remember where it was started from with a bit of code. But the whole point of a daemon is to detach it from a terminal so wouldn't it be easier to just write errors to a log file like most daemons do?
Are the users logging off and back on while the daemon runs? What if they log on the next time and they get a different terminal? Then your daemon will be printing messages to a terminal that's not logged in or even possibly printing the messages to someone else that logged in on that terminal.
And if they're not logging off then why daemonize the program? Just run it in the background.
If they have a terminal that's dedicates to just watching error messages (again, if they're logging off/on I think you'll have a syncronization issue with making sure they get that same terminal ID) then why don't they just tail -f the log file so it automatically shows the error as soon as it's logged?
In every scenario I can think of, what you're proposing to do just doesn't make sense to me.
More or less by definition, a daemon has no access to its terminal. And if you need to talk to the terminal, why run it in daemon mode?
Code below shows itsme86's idea about remembering the the terminal the daemon was started from. But note (as itsme86 pointed out as well) that the message may appear in the wrong terminal if the user who started the program already logged of.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
/* The message to write to the terminal */
#define MESSAGE "Somethin...\n"
int main(int argc, char **argv)
{
int fd;
char *termfile;
printf("Just started. I'm not a daemon yet\n");
/* Remember the terminal device file we're on at this moment. */
termfile = strdup(ttyname(1));
if (termfile == NULL) { /* Catch just in case error. */
fprintf(stderr, "Error: Out of memory while copying tty-name\n");
return 1;
}
printf("My terminal device file is: %s\n", termfile);
/* Daemonize */
printf("Will try to run as a daemon now...\n");
if (daemon(0,0) < 0) {
perror(*argv);
return 1;
}
syslog(LOG_INFO, "I'm a daemon now.");
syslog(LOG_INFO, "I will try to write something to the terminal I was started from.");
/* Open filedescriptor to the terminal file */
fd = open(termfile, O_WRONLY | O_NOCTTY);
if (fd < 0) {
syslog(LOG_ERR, "Could not open %s. Exit..", termfile);
return 2;
}
/* Write something to the terminal */
if (write(fd, MESSAGE, strlen(MESSAGE)) < 0) {
syslog(LOG_ERR, "Could not write to %s. Exit..", termfile);
return 2;
}
syslog(LOG_INFO, "Success! I suppose the message is on the terminal now...");
free(termfile);
return 0;
}
U can send ur messages from daemon to the machine's console. The associated device is /dev/console. Simply open the device using open system call and write the text u want to display. It will be displayed on the screen. But u need to have root permissions to do this !!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.