2 * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
4 * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
5 * Copyright (c) 2009 Verari Systems Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This code is derived from an example daemon code from
23 * http://en.wikipedia.org/wiki/Daemon_(computer_software)#Sample_Program_in_C_on_Linux
24 * (accessed 2009-05-05)
26 * So it is additionally licensed under the GNU Free Documentation License:
28 * Permission is granted to copy, distribute and/or modify this document
29 * under the terms of the GNU Free Documentation License, Version 1.2
30 * or any later version published by the Free Software Foundation;
31 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
32 * A copy of the license is included in the section entitled "GNU
33 * Free Documentation License".
35 * Send feedback to <linux-can@vger.kernel.org>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/types.h>
54 /* default slcan line discipline since Kernel 2.6.25 */
55 #define LDISC_N_SLCAN 17
57 /* Change this to whatever your daemon is called */
58 #define DAEMON_NAME "slcand"
60 /* Change this to the user under which to run */
61 #define RUN_AS_USER "root"
63 /* The length of ttypath buffer */
64 #define TTYPATH_LENGTH 64
66 /* The length of pidfile name buffer */
67 #define PIDFILE_LENGTH 64
69 #define EXIT_SUCCESS 0
70 #define EXIT_FAILURE 1
72 void print_usage(char *prg)
74 fprintf(stderr, "\nUsage: %s tty [name]\n\n", prg);
75 fprintf(stderr, "Example:\n");
76 fprintf(stderr, "%s ttyUSB1\n", prg);
77 fprintf(stderr, "%s ttyS0 can0\n", prg);
78 fprintf(stderr, "\n");
82 static void child_handler (int signum)
98 static void daemonize (const char *lockfile, char *tty)
100 pid_t pid, sid, parent;
103 char const *pidprefix = "/var/run/";
104 char const *pidsuffix = ".pid";
105 char pidfile[PIDFILE_LENGTH];
107 snprintf(pidfile, PIDFILE_LENGTH, "%s%s-%s%s", pidprefix, DAEMON_NAME, tty, pidsuffix);
109 /* already a daemon */
113 /* Create the lock file as the current user */
114 if (lockfile && lockfile[0])
116 lfp = open (lockfile, O_RDWR | O_CREAT, 0640);
119 syslog (LOG_ERR, "unable to create lock file %s, code=%d (%s)",
120 lockfile, errno, strerror (errno));
125 /* Drop user if there is one, and we were run as root */
126 if (getuid () == 0 || geteuid () == 0)
128 struct passwd *pw = getpwnam (RUN_AS_USER);
131 //syslog (LOG_NOTICE, "setting user to " RUN_AS_USER);
136 /* Trap signals that we expect to receive */
137 signal (SIGCHLD, child_handler);
138 signal (SIGUSR1, child_handler);
139 signal (SIGALRM, child_handler);
141 /* Fork off the parent process */
145 syslog (LOG_ERR, "unable to fork daemon, code=%d (%s)",
146 errno, strerror (errno));
149 /* If we got a good PID, then we can exit the parent process. */
153 /* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
154 for two seconds to elapse (SIGALRM). pause() should not return. */
161 /* At this point we are executing as the child process */
164 /* Cancel certain signals */
165 signal (SIGCHLD, SIG_DFL); /* A child process dies */
166 signal (SIGTSTP, SIG_IGN); /* Various TTY signals */
167 signal (SIGTTOU, SIG_IGN);
168 signal (SIGTTIN, SIG_IGN);
169 signal (SIGHUP, SIG_IGN); /* Ignore hangup signal */
170 signal (SIGTERM, SIG_DFL); /* Die on SIGTERM */
172 /* Change the file mode mask */
175 /* Create a new SID for the child process */
179 syslog (LOG_ERR, "unable to create a new session, code %d (%s)",
180 errno, strerror (errno));
184 pFile = fopen (pidfile,"w");
187 syslog (LOG_ERR, "unable to create pid file %s, code=%d (%s)",
188 pidfile, errno, strerror (errno));
191 fprintf (pFile, "%d\n", sid);
194 /* Change the current working directory. This prevents the current
195 directory from being locked; hence not being able to remove it. */
196 if ((chdir ("/")) < 0)
198 syslog (LOG_ERR, "unable to change directory to %s, code %d (%s)",
199 "/", errno, strerror (errno));
203 /* Redirect standard files to /dev/null */
204 freopen ("/dev/null", "r", stdin);
205 freopen ("/dev/null", "w", stdout);
206 freopen ("/dev/null", "w", stderr);
208 /* Tell the parent process that we are A-okay */
209 kill (parent, SIGUSR1);
212 int main (int argc, char *argv[])
215 char ttypath[TTYPATH_LENGTH];
216 char const *devprefix = "/dev/";
218 char buf[IFNAMSIZ+1];
220 /* Initialize the logging interface */
221 openlog (DAEMON_NAME, LOG_PID, LOG_LOCAL5);
223 /* See how we're called */
226 } else if (argc == 3) {
229 if (strlen(name) > IFNAMSIZ-1)
230 print_usage(argv[0]);
232 print_usage(argv[0]);
235 /* Prepare the tty device name string */
237 pch = strstr (tty, devprefix);
239 print_usage(argv[0]);
242 snprintf (ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
243 syslog (LOG_INFO, "starting on TTY device %s", ttypath);
246 daemonize ("/var/lock/" DAEMON_NAME, tty);
248 /* Now we are a daemon -- do the work for which we were paid */
250 int ldisc = LDISC_N_SLCAN;
252 if ((fd = open (ttypath, O_WRONLY | O_NOCTTY )) < 0) {
253 syslog (LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
258 /* set slcan like discipline on given tty */
259 if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
260 perror("ioctl TIOCSETD");
264 /* retrieve the name of the created CAN netdevice */
265 if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
266 perror("ioctl SIOCGIFNAME");
270 syslog (LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
272 /* try to rename the created netdevice */
275 int s = socket(PF_INET, SOCK_DGRAM, 0);
277 perror("socket for interface rename");
279 strncpy (ifr.ifr_name, buf, IFNAMSIZ);
280 strncpy (ifr.ifr_newname, name, IFNAMSIZ);
282 if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
283 syslog (LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
284 perror("ioctl SIOCSIFNAME rename");
287 syslog (LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
295 sleep(60); /* wait 60 seconds */
299 syslog (LOG_NOTICE, "terminated on %s", ttypath);