2

I have written a deamon in C working on Linux, and now I need to be able to send short messages to Linux console like the commad "wall" does, or how init does when it reboots the system.

How to do that from inside my program ?

best regards

Marek

user2018761
  • 306
  • 5
  • 14
  • Did you consider using [syslog(3)](http://man7.org/linux/man-pages/man3/syslog.3.html) and configuring your `syslogd` to show it on consoles? – Basile Starynkevitch Jan 29 '14 at 11:14
  • Yes, one of option I heard of just now, would like to avoid this because of 4 different syslog daemons like syslog-ng, rsyslog... – user2018761 Jan 29 '14 at 11:29
  • Leave the configuration of syslog daemons to sysadmins. But document exactly how your software is using `syslog(3)`; also, some systems don't have consoles watched by humans.... – Basile Starynkevitch Jan 29 '14 at 11:39

4 Answers4

2

The current console linux device is /dev/console, but you need to be root to write to this file. See the man page for console for more info, but here is an extract:

Common ways to start a process on a console are:

  • (a) tell init(8) (in inittab(5)) to start a mingetty(8) (or agetty(8)) on the console;
  • (b) ask openvt(1) to start a process on the console;
  • (c) start X — it will find the first unused console, and display its output there.(There is also the ancient doshell(8).)
suspectus
  • 16,548
  • 8
  • 49
  • 57
0

To send messages to multiple terminals/consoles use the ttymsg() on various tty nodes.

For a good example on how to use this is your C program, checkout the source of the walk command. We can see precisely how it prepares a message buffer and sends it as a broadcast to various terminals of all the currently logged-in users.

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
  • `ttymsg` is usually not in Linux: not in libc, not documented in man pages on Linux. Seems to be BSD specific! – Basile Starynkevitch Jan 29 '14 at 11:12
  • **ttymsg()** is availed by adding `#include ` in your program. It is part of [**`libutil`** which is available on debian](http://packages.debian.org/cgi-bin/search_contents.pl?word=libutil.so.1&searchmode=searchfiles&case=insensitive&version=unstable&arch=i386). – TheCodeArtist Jan 29 '14 at 11:42
  • can't locate ttymsg.h http://packages.debian.org/search?mode=filename&suite=wheezy&section=all&arch=i386&searchon=contents&keywords=ttymsg.h – user2018761 Jan 29 '14 at 11:51
  • Hmmm, right! You could probably get away with simply adding this line in your program `char *ttymsg(struct iovec *iov, int iovcnt, char *line, int tmout);` to declare the external function. Ensure that your link with **`libutil`** when building your program. – TheCodeArtist Jan 29 '14 at 12:13
  • ...or, one could go ahead and simply add the implementation of `ttymsg()` as done in this [project](https://github.com/flightaware/tclsyslogd/tree/master/src). – TheCodeArtist Jan 29 '14 at 12:43
  • @TheCodeArtist - I have used ttymsg implementation from project you gave link to, combined with piece of source from walk command it works as expected, I'm logged on a few terminals by ssh and all of them receive messages from my daemon. Thanks! – user2018761 Jan 29 '14 at 13:48
  • TheCodeArtist - Just one question, earlier someone suggested me to write my own function that looks into /dev/pts/ filesystem and writes data to each found file/device. Does this ttymsg works essentially with the same idea ? – user2018761 Jan 29 '14 at 13:52
  • More or less the same. However writing directly to `/dev/pts` needs to be done with care as the characters simply appear upon the terminal without really being fed to the shell. Hence format the message such that it is obvious that this is a broadcast message generated by the system after which the user may continue with his interrupted work (eg. interrupted in the middle of a command, continues typing the rest of it and not start from scratch). – TheCodeArtist Jan 29 '14 at 14:23
0

For sending ocasional short messages, the best (most portable, simplest) way to do it would be just to run wall(1).

vonbrand
  • 11,412
  • 8
  • 32
  • 52
  • ...and one might take a look at [**this answer**](http://stackoverflow.com/a/17827426/319204) to run a command (`wall` in this case) as root from our regular C program. – TheCodeArtist Jan 29 '14 at 14:27
0

I would like to avoid executing other binaries from my program. Solution with ttymsg and code from wall program works for me well.

Thanks for help Marek

user2018761
  • 306
  • 5
  • 14