]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - slcand.c
can-calc-bit-timing: add more can controller definitions and ref clocks
[sojka/can-utils.git] / slcand.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
7  *
8  * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
9  * Copyright (c) 2009 Verari Systems Inc.
10  *
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.
15  *
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.
20  *
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
24  *
25  * This code is derived from an example daemon code from
26  *
27  * http://en.wikipedia.org/wiki/Daemon_(computer_software)#Sample_Program_in_C_on_Linux
28  * (accessed 2009-05-05)
29  *
30  * So it is additionally licensed under the GNU Free Documentation License:
31  *
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".
38  *
39  * Send feedback to <socketcan-users@lists.berlios.de>
40  *
41  */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <syslog.h>
51 #include <errno.h>
52 #include <pwd.h>
53 #include <signal.h>
54 #include <sys/ioctl.h>
55
56 /* default slcan line discipline since Kernel 2.6.25 */
57 #define LDISC_N_SLCAN 17
58
59 /* Change this to whatever your daemon is called */
60 #define DAEMON_NAME "slcand"
61
62 /* Change this to the user under which to run */
63 #define RUN_AS_USER "root"
64
65 #define EXIT_SUCCESS 0
66 #define EXIT_FAILURE 1
67
68 void print_usage(char *prg)
69 {
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");
75         exit(EXIT_FAILURE);
76 }
77
78 static void child_handler (int signum)
79 {
80         switch (signum)
81         {
82         case SIGALRM:
83                 exit (EXIT_FAILURE);
84                 break;
85         case SIGUSR1:
86                 exit (EXIT_SUCCESS);
87                 break;
88         case SIGCHLD:
89                 exit (EXIT_FAILURE);
90                 break;
91         }
92 }
93
94 static void daemonize (const char *lockfile, char *tty)
95 {
96         pid_t pid, sid, parent;
97         int lfp = -1;
98         FILE * pFile;
99         char const *pidprefix = "/var/run/";
100         char const *pidsuffix = ".pid";
101         char *pidfile;
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);
104
105         /* already a daemon */
106         if (getppid () == 1)
107                 return;
108
109         /* Create the lock file as the current user */
110         if (lockfile && lockfile[0])
111         {
112                 lfp = open (lockfile, O_RDWR | O_CREAT, 0640);
113                 if (lfp < 0)
114                 {
115                         syslog (LOG_ERR, "unable to create lock file %s, code=%d (%s)",
116                                 lockfile, errno, strerror (errno));
117                         exit (EXIT_FAILURE);
118                 }
119         }
120
121         /* Drop user if there is one, and we were run as root */
122         if (getuid () == 0 || geteuid () == 0)
123         {
124                 struct passwd *pw = getpwnam (RUN_AS_USER);
125                 if (pw)
126                 {
127                         syslog (LOG_NOTICE, "setting user to " RUN_AS_USER);
128                         setuid (pw->pw_uid);
129                 }
130         }
131
132         /* Trap signals that we expect to receive */
133         signal (SIGCHLD, child_handler);
134         signal (SIGUSR1, child_handler);
135         signal (SIGALRM, child_handler);
136
137         /* Fork off the parent process */
138         pid = fork ();
139         if (pid < 0)
140         {
141                 syslog (LOG_ERR, "unable to fork daemon, code=%d (%s)",
142                         errno, strerror (errno));
143                 exit (EXIT_FAILURE);
144         }
145         /* If we got a good PID, then we can exit the parent process. */
146         if (pid > 0)
147         {
148
149                 /* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
150                    for two seconds to elapse (SIGALRM).  pause() should not return. */
151                 alarm (2);
152                 pause ();
153
154                 exit (EXIT_FAILURE);
155         }
156
157         /* At this point we are executing as the child process */
158         parent = getppid ();
159
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 */
167
168         /* Change the file mode mask */
169         umask (0);
170
171         /* Create a new SID for the child process */
172         sid = setsid ();
173         if (sid < 0)
174         {
175                 syslog (LOG_ERR, "unable to create a new session, code %d (%s)",
176                         errno, strerror (errno));
177                 exit (EXIT_FAILURE);
178         }
179
180         pFile = fopen (pidfile,"w");
181         if (pFile < 0)
182         {
183                 syslog (LOG_ERR, "unable to create pid file %s, code=%d (%s)",
184                         pidfile, errno, strerror (errno));
185                 exit (EXIT_FAILURE);
186         }
187         fprintf (pFile, "%d\n", sid);
188         fclose (pFile);
189
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)
193         {
194                 syslog (LOG_ERR, "unable to change directory to %s, code %d (%s)",
195                         "/", errno, strerror (errno));
196                 exit (EXIT_FAILURE);
197         }
198
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);
203
204         /* Tell the parent process that we are A-okay */
205         kill (parent, SIGUSR1);
206 }
207
208 int main (int argc, char *argv[])
209 {
210         char *tty;
211         char *ttypath;
212         char const *devprefix = "/dev/";
213
214         /* Initialize the logging interface */
215         openlog (DAEMON_NAME, LOG_PID, LOG_LOCAL5);
216
217         /* See how we're called */
218         if (argc != 2)
219                 print_usage(argv[0]);
220         tty = argv[1];
221
222         /* Prepare the tty device name string */
223         char * pch;
224         pch = strstr (tty, devprefix);
225         if (pch == tty) {
226                 print_usage(argv[0]);
227         }
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);
231
232         /* Daemonize */
233         daemonize ("/var/lock/" DAEMON_NAME, tty);
234
235         /* Now we are a daemon -- do the work for which we were paid */
236         int fd;
237         int ldisc = LDISC_N_SLCAN;
238
239         if ((fd = open (ttypath, O_WRONLY | O_NOCTTY )) < 0) {
240                 perror(ttypath);
241                 exit(1);
242         }
243
244         if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
245                 perror("ioctl");
246                 exit(1);
247         }
248
249         /* The Big Loop */
250         while (1) {
251                 sleep(60); /* wait 60 seconds */
252         }
253
254         /* Finish up */
255         syslog (LOG_NOTICE, "terminated on %s", ttypath);
256         closelog ();
257         return 0;
258 }