6 * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
8 * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
9 * Copyright (c) 2009 Verari Systems Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This code is derived from an example daemon code from
27 * http://en.wikipedia.org/wiki/Daemon_(computer_software)#Sample_Program_in_C_on_Linux
28 * (accessed 2009-05-05)
30 * So it is additionally licensed under the GNU Free Documentation License:
32 * Permission is granted to copy, distribute and/or modify this document
33 * under the terms of the GNU Free Documentation License, Version 1.2
34 * or any later version published by the Free Software Foundation;
35 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
36 * A copy of the license is included in the section entitled "GNU
37 * Free Documentation License".
39 * Send feedback to <linux-can@vger.kernel.org>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54 #include <sys/types.h>
58 /* default slcan line discipline since Kernel 2.6.25 */
59 #define LDISC_N_SLCAN 17
61 /* Change this to whatever your daemon is called */
62 #define DAEMON_NAME "slcand"
64 /* Change this to the user under which to run */
65 #define RUN_AS_USER "root"
67 /* The length of ttypath buffer */
68 #define TTYPATH_LENGTH 64
70 /* The length of pidfile name buffer */
71 #define PIDFILE_LENGTH 64
73 #define EXIT_SUCCESS 0
74 #define EXIT_FAILURE 1
76 void print_usage(char *prg)
78 fprintf(stderr, "\nUsage: %s tty [name]\n\n", prg);
79 fprintf(stderr, "Example:\n");
80 fprintf(stderr, "%s ttyUSB1\n", prg);
81 fprintf(stderr, "%s ttyS0 can0\n", prg);
82 fprintf(stderr, "\n");
86 static void child_handler (int signum)
102 static void daemonize (const char *lockfile, char *tty, char *name)
104 pid_t pid, sid, parent;
107 char const *pidprefix = "/var/run/";
108 char const *pidsuffix = ".pid";
109 char pidfile[PIDFILE_LENGTH];
111 snprintf(pidfile, PIDFILE_LENGTH, "%s%s-%s%s", pidprefix, DAEMON_NAME, tty, pidsuffix);
113 /* already a daemon */
117 /* Create the lock file as the current user */
118 if (lockfile && lockfile[0])
120 lfp = open (lockfile, O_RDWR | O_CREAT, 0640);
123 syslog (LOG_ERR, "unable to create lock file %s, code=%d (%s)",
124 lockfile, errno, strerror (errno));
129 /* Drop user if there is one, and we were run as root */
130 if (getuid () == 0 || geteuid () == 0)
132 struct passwd *pw = getpwnam (RUN_AS_USER);
135 //syslog (LOG_NOTICE, "setting user to " RUN_AS_USER);
140 /* Trap signals that we expect to receive */
141 signal (SIGCHLD, child_handler);
142 signal (SIGUSR1, child_handler);
143 signal (SIGALRM, child_handler);
145 /* Fork off the parent process */
149 syslog (LOG_ERR, "unable to fork daemon, code=%d (%s)",
150 errno, strerror (errno));
153 /* If we got a good PID, then we can exit the parent process. */
157 /* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
158 for two seconds to elapse (SIGALRM). pause() should not return. */
165 /* At this point we are executing as the child process */
168 /* Cancel certain signals */
169 signal (SIGCHLD, SIG_DFL); /* A child process dies */
170 signal (SIGTSTP, SIG_IGN); /* Various TTY signals */
171 signal (SIGTTOU, SIG_IGN);
172 signal (SIGTTIN, SIG_IGN);
173 signal (SIGHUP, SIG_IGN); /* Ignore hangup signal */
174 signal (SIGTERM, SIG_DFL); /* Die on SIGTERM */
176 /* Change the file mode mask */
179 /* Create a new SID for the child process */
183 syslog (LOG_ERR, "unable to create a new session, code %d (%s)",
184 errno, strerror (errno));
188 pFile = fopen (pidfile,"w");
191 syslog (LOG_ERR, "unable to create pid file %s, code=%d (%s)",
192 pidfile, errno, strerror (errno));
195 fprintf (pFile, "%d\n", sid);
198 /* Change the current working directory. This prevents the current
199 directory from being locked; hence not being able to remove it. */
200 if ((chdir ("/")) < 0)
202 syslog (LOG_ERR, "unable to change directory to %s, code %d (%s)",
203 "/", errno, strerror (errno));
207 /* Redirect standard files to /dev/null */
208 freopen ("/dev/null", "r", stdin);
209 freopen ("/dev/null", "w", stdout);
210 freopen ("/dev/null", "w", stderr);
212 /* Tell the parent process that we are A-okay */
213 kill (parent, SIGUSR1);
216 int main (int argc, char *argv[])
219 char ttypath[TTYPATH_LENGTH];
220 char const *devprefix = "/dev/";
222 char buf[IFNAMSIZ+1];
224 /* Initialize the logging interface */
225 openlog (DAEMON_NAME, LOG_PID, LOG_LOCAL5);
227 /* See how we're called */
230 } else if (argc == 3) {
233 if (strlen(name) > IFNAMSIZ-1)
234 print_usage(argv[0]);
236 print_usage(argv[0]);
239 /* Prepare the tty device name string */
241 pch = strstr (tty, devprefix);
243 print_usage(argv[0]);
246 snprintf (ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
247 syslog (LOG_INFO, "starting on TTY device %s", ttypath);
250 daemonize ("/var/lock/" DAEMON_NAME, tty, name);
252 /* Now we are a daemon -- do the work for which we were paid */
254 int ldisc = LDISC_N_SLCAN;
256 if ((fd = open (ttypath, O_WRONLY | O_NOCTTY )) < 0) {
257 syslog (LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
262 /* set slcan like discipline on given tty */
263 if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
264 perror("ioctl TIOCSETD");
268 /* retrieve the name of the created CAN netdevice */
269 if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
270 perror("ioctl SIOCGIFNAME");
274 syslog (LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
276 /* try to rename the created netdevice */
279 int s = socket(PF_INET, SOCK_DGRAM, 0);
281 perror("socket for interface rename");
283 strncpy (ifr.ifr_name, buf, IFNAMSIZ);
284 strncpy (ifr.ifr_newname, name, IFNAMSIZ);
286 if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
287 syslog (LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
288 perror("ioctl SIOCSIFNAME rename");
291 syslog (LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
299 sleep(60); /* wait 60 seconds */
303 syslog (LOG_NOTICE, "terminated on %s", ttypath);