]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - slcand.c
can-utils: AOSP build clean up
[sojka/can-utils.git] / slcand.c
1 /*
2  * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
3  *
4  * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
5  * Copyright (c) 2009 Verari Systems Inc.
6  *
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.
11  *
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.
16  *
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
20  *
21  * This code is derived from an example daemon code from
22  *
23  * http://en.wikipedia.org/wiki/Daemon_(computer_software)#Sample_Program_in_C_on_Linux
24  * (accessed 2009-05-05)
25  *
26  * So it is additionally licensed under the GNU Free Documentation License:
27  *
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".
34  *
35  * Send feedback to <linux-can@vger.kernel.org>
36  *
37  */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <syslog.h>
45 #include <errno.h>
46 #include <pwd.h>
47 #include <signal.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <net/if.h>
53
54 /* default slcan line discipline since Kernel 2.6.25 */
55 #define LDISC_N_SLCAN 17
56
57 /* Change this to whatever your daemon is called */
58 #define DAEMON_NAME "slcand"
59
60 /* Change this to the user under which to run */
61 #define RUN_AS_USER "root"
62
63 /* The length of ttypath buffer */
64 #define TTYPATH_LENGTH  64
65
66 /* The length of pidfile name buffer */
67 #define PIDFILE_LENGTH  64
68
69 #define EXIT_SUCCESS 0
70 #define EXIT_FAILURE 1
71
72 void print_usage(char *prg)
73 {
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");
79         exit(EXIT_FAILURE);
80 }
81
82 static void child_handler (int signum)
83 {
84         switch (signum)
85         {
86         case SIGALRM:
87                 exit (EXIT_FAILURE);
88                 break;
89         case SIGUSR1:
90                 exit (EXIT_SUCCESS);
91                 break;
92         case SIGCHLD:
93                 exit (EXIT_FAILURE);
94                 break;
95         }
96 }
97
98 static void daemonize (const char *lockfile, char *tty)
99 {
100         pid_t pid, sid, parent;
101         int lfp = -1;
102         FILE * pFile;
103         char const *pidprefix = "/var/run/";
104         char const *pidsuffix = ".pid";
105         char pidfile[PIDFILE_LENGTH];
106
107         snprintf(pidfile, PIDFILE_LENGTH, "%s%s-%s%s", pidprefix, DAEMON_NAME, tty, pidsuffix);
108
109         /* already a daemon */
110         if (getppid () == 1)
111                 return;
112
113         /* Create the lock file as the current user */
114         if (lockfile && lockfile[0])
115         {
116                 lfp = open (lockfile, O_RDWR | O_CREAT, 0640);
117                 if (lfp < 0)
118                 {
119                         syslog (LOG_ERR, "unable to create lock file %s, code=%d (%s)",
120                                 lockfile, errno, strerror (errno));
121                         exit (EXIT_FAILURE);
122                 }
123         }
124
125         /* Drop user if there is one, and we were run as root */
126         if (getuid () == 0 || geteuid () == 0)
127         {
128                 struct passwd *pw = getpwnam (RUN_AS_USER);
129                 if (pw)
130                 {
131                         //syslog (LOG_NOTICE, "setting user to " RUN_AS_USER);
132                         setuid (pw->pw_uid);
133                 }
134         }
135
136         /* Trap signals that we expect to receive */
137         signal (SIGCHLD, child_handler);
138         signal (SIGUSR1, child_handler);
139         signal (SIGALRM, child_handler);
140
141         /* Fork off the parent process */
142         pid = fork ();
143         if (pid < 0)
144         {
145                 syslog (LOG_ERR, "unable to fork daemon, code=%d (%s)",
146                         errno, strerror (errno));
147                 exit (EXIT_FAILURE);
148         }
149         /* If we got a good PID, then we can exit the parent process. */
150         if (pid > 0)
151         {
152
153                 /* Wait for confirmation from the child via SIGTERM or SIGCHLD, or
154                    for two seconds to elapse (SIGALRM).  pause() should not return. */
155                 alarm (2);
156                 pause ();
157
158                 exit (EXIT_FAILURE);
159         }
160
161         /* At this point we are executing as the child process */
162         parent = getppid ();
163
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 */
171
172         /* Change the file mode mask */
173         umask (0);
174
175         /* Create a new SID for the child process */
176         sid = setsid ();
177         if (sid < 0)
178         {
179                 syslog (LOG_ERR, "unable to create a new session, code %d (%s)",
180                         errno, strerror (errno));
181                 exit (EXIT_FAILURE);
182         }
183
184         pFile = fopen (pidfile,"w");
185         if (pFile == NULL)
186         {
187                 syslog (LOG_ERR, "unable to create pid file %s, code=%d (%s)",
188                         pidfile, errno, strerror (errno));
189                 exit (EXIT_FAILURE);
190         }
191         fprintf (pFile, "%d\n", sid);
192         fclose (pFile);
193
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)
197         {
198                 syslog (LOG_ERR, "unable to change directory to %s, code %d (%s)",
199                         "/", errno, strerror (errno));
200                 exit (EXIT_FAILURE);
201         }
202
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);
207
208         /* Tell the parent process that we are A-okay */
209         kill (parent, SIGUSR1);
210 }
211
212 int main (int argc, char *argv[])
213 {
214         char *tty = NULL;
215         char ttypath[TTYPATH_LENGTH];
216         char const *devprefix = "/dev/";
217         char *name = NULL;
218         char buf[IFNAMSIZ+1];
219
220         /* Initialize the logging interface */
221         openlog (DAEMON_NAME, LOG_PID, LOG_LOCAL5);
222
223         /* See how we're called */
224         if (argc == 2) {
225                 tty = argv[1];
226         } else if (argc == 3) {
227                 tty = argv[1];
228                 name = argv[2];
229                 if (strlen(name) > IFNAMSIZ-1)
230                         print_usage(argv[0]);
231         } else {
232                 print_usage(argv[0]);
233         }
234
235         /* Prepare the tty device name string */
236         char * pch;
237         pch = strstr (tty, devprefix);
238         if (pch == tty) {
239                 print_usage(argv[0]);
240         }
241
242         snprintf (ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
243         syslog (LOG_INFO, "starting on TTY device %s", ttypath);
244
245         /* Daemonize */
246         daemonize ("/var/lock/" DAEMON_NAME, tty);
247
248         /* Now we are a daemon -- do the work for which we were paid */
249         int fd;
250         int ldisc = LDISC_N_SLCAN;
251
252         if ((fd = open (ttypath, O_WRONLY | O_NOCTTY )) < 0) {
253             syslog (LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
254                 perror(ttypath);
255                 exit(1);
256         }
257
258         /* set slcan like discipline on given tty */
259         if (ioctl (fd, TIOCSETD, &ldisc) < 0) {
260                 perror("ioctl TIOCSETD");
261                 exit(1);
262         }
263         
264         /* retrieve the name of the created CAN netdevice */
265         if (ioctl (fd, SIOCGIFNAME, buf) < 0) {
266                 perror("ioctl SIOCGIFNAME");
267                 exit(1);
268         }
269
270         syslog (LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
271         
272         /* try to rename the created netdevice */
273         if (name) {
274                 struct ifreq ifr;
275                 int s = socket(PF_INET, SOCK_DGRAM, 0);
276                 if (s < 0)
277                         perror("socket for interface rename");
278                 else {
279                         strncpy (ifr.ifr_name, buf, IFNAMSIZ);
280                         strncpy (ifr.ifr_newname, name, IFNAMSIZ);
281
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");
285                 exit(1);
286                         } else
287                                 syslog (LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
288
289                         close(s);
290                 }       
291         }
292
293         /* The Big Loop */
294         while (1) {
295                 sleep(60); /* wait 60 seconds */
296         }
297
298         /* Finish up */
299         syslog (LOG_NOTICE, "terminated on %s", ttypath);
300         closelog ();
301         return 0;
302 }