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 <socketcan-users@lists.berlios.de>
47 #include <sys/types.h>
54 #include <sys/ioctl.h>
56 /* default slcan line discipline since Kernel 2.6.25 */
57 #define LDISC_N_SLCAN 17
59 /* Change this to whatever your daemon is called */
60 #define DAEMON_NAME "slcand"
62 /* Change this to the user under which to run */
63 #define RUN_AS_USER "root"
65 #define EXIT_SUCCESS 0
66 #define EXIT_FAILURE 1
68 void print_usage(char *prg)
70 fprintf(stderr, "\nUsage: %s tty\n\n", prg);
71 fprintf(stderr, "Example:\n");
72 fprintf(stderr, "%s ttyUSB1\n", prg);
73 fprintf(stderr, "%s ttyS0\n", prg);
74 fprintf(stderr, "\n");
78 static void child_handler (int signum)
94 static void daemonize (const char *lockfile, char *tty)
96 pid_t pid, sid, parent;
99 char const *pidprefix = "/var/run/";
100 char const *pidsuffix = ".pid";
102 pidfile = malloc((strlen(pidprefix) +strlen(DAEMON_NAME) + strlen(tty) + strlen(pidsuffix) + 1) * sizeof(char));
103 sprintf(pidfile, "%s%s-%s%s", pidprefix, DAEMON_NAME, tty, pidsuffix);
105 /* already a daemon */
109 /* Create the lock file as the current user */
110 if (lockfile && lockfile[0])
112 lfp = open (lockfile, O_RDWR | O_CREAT, 0640);
115 syslog (LOG_ERR, "unable to create lock file %s, code=%d (%s)",
116 lockfile, errno, strerror (errno));
121 /* Drop user if there is one, and we were run as root */
122 if (getuid () == 0 || geteuid () == 0)
124 struct passwd *pw = getpwnam (RUN_AS_USER);
127 syslog (LOG_NOTICE, "setting user to " RUN_AS_USER);
132 /* Trap signals that we expect to receive */
133 signal (SIGCHLD, child_handler);
134 signal (SIGUSR1, child_handler);
135 signal (SIGALRM, child_handler);
137 /* Fork off the parent process */
141 syslog (LOG_ERR, "unable to fork daemon, code=%d (%s)",
142 errno, strerror (errno));
145 /* If we got a good PID, then we can exit the parent process. */
149 /* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
150 for two seconds to elapse (SIGALRM). pause() should not return. */
157 /* At this point we are executing as the child process */
160 /* Cancel certain signals */
161 signal (SIGCHLD, SIG_DFL); /* A child process dies */
162 signal (SIGTSTP, SIG_IGN); /* Various TTY signals */
163 signal (SIGTTOU, SIG_IGN);
164 signal (SIGTTIN, SIG_IGN);
165 signal (SIGHUP, SIG_IGN); /* Ignore hangup signal */
166 signal (SIGTERM, SIG_DFL); /* Die on SIGTERM */
168 /* Change the file mode mask */
171 /* Create a new SID for the child process */
175 syslog (LOG_ERR, "unable to create a new session, code %d (%s)",
176 errno, strerror (errno));
180 pFile = fopen (pidfile,"w");
183 syslog (LOG_ERR, "unable to create pid file %s, code=%d (%s)",
184 pidfile, errno, strerror (errno));
187 fprintf (pFile, "%d\n", sid);
190 /* Change the current working directory. This prevents the current
191 directory from being locked; hence not being able to remove it. */
192 if ((chdir ("/")) < 0)
194 syslog (LOG_ERR, "unable to change directory to %s, code %d (%s)",
195 "/", errno, strerror (errno));
199 /* Redirect standard files to /dev/null */
200 freopen ("/dev/null", "r", stdin);
201 freopen ("/dev/null", "w", stdout);
202 freopen ("/dev/null", "w", stderr);
204 /* Tell the parent process that we are A-okay */
205 kill (parent, SIGUSR1);
208 int main (int argc, char *argv[])
212 char const *devprefix = "/dev/";
214 /* Initialize the logging interface */
215 openlog (DAEMON_NAME, LOG_PID, LOG_LOCAL5);
217 /* See how we're called */
219 print_usage(argv[0]);
222 /* Prepare the tty device name string */
224 pch = strstr (tty, devprefix);
226 print_usage(argv[0]);
228 ttypath = malloc((strlen(devprefix) + strlen(tty)) * sizeof(char));
229 sprintf (ttypath, "%s%s", devprefix, tty);
230 syslog (LOG_INFO, "starting on TTY device %s", ttypath);
233 daemonize ("/var/lock/" DAEMON_NAME, tty);
235 /* Now we are a daemon -- do the work for which we were paid */
237 int ldisc = LDISC_N_SLCAN;
239 if ((fd = open (ttypath, O_WRONLY | O_NOCTTY )) < 0) {
244 if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
251 sleep(60); /* wait 60 seconds */
255 syslog (LOG_NOTICE, "terminated on %s", ttypath);