chiark / gitweb /
disobedience can stop/start a background rtp player now
[disorder] / disobedience / rtp.c
1 /*
2  * This file is part of Disobedience
3  * Copyright (C) 2007 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file disobedience/rtp.c
21  * @brief RTP player support for Disobedience
22  */
23
24 #include "disobedience.h"
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/un.h>
29
30 /** @brief Path to RTP player's control socket */
31 static char *rtp_socket;
32
33 /** @brief Path to RTP player's logfile */
34 static char *rtp_log;
35
36 /** @brief Initialize @ref rtp_socket and @ref rtp_log if necessary */
37 static void rtp_init(void) {
38   if(!rtp_socket) {
39     const char *home = getenv("HOME");
40     char *dir;
41
42     byte_xasprintf(&dir, "%s/.disorder", home);
43     mkdir(dir, 02700);
44     byte_xasprintf(&rtp_socket, "%s/rtp", dir);
45     byte_xasprintf(&rtp_log, "%s/rtp.log", dir);
46   }
47 }
48
49 /** @brief Return a connection to the RTP player's control socket */
50 static FILE *rtp_connect(void) {
51   struct sockaddr_un un;
52   int fd;
53   FILE *fp;
54
55   rtp_init();
56   memset(&un, 0, sizeof un);
57   un.sun_family = AF_UNIX;
58   strcpy(un.sun_path, rtp_socket);
59   fd = xsocket(PF_UNIX, SOCK_STREAM, 0);
60   if(connect(fd, (const struct sockaddr *)&un, sizeof un) < 0) {
61     /* Connection refused just means that the player quit without deleting its
62      * socket */
63     switch(errno) {
64     case ECONNREFUSED:
65     case ENOENT:
66       break;
67     default:
68       /* Anything else may be a problem */
69       fpopup_msg(GTK_MESSAGE_ERROR, "connecting to %s: %s",
70                  rtp_socket, strerror(errno));
71       break;
72     }
73     close(fd);
74     return 0;
75   }
76   if(!(fp = fdopen(fd, "r+"))) {
77     fpopup_msg(GTK_MESSAGE_ERROR, "error calling fdopen: %s", strerror(errno));
78     close(fd);
79     return 0;
80   }
81   return fp;
82 }
83
84 /** @brief Return non-0 iff the RTP player is running */
85 int rtp_running(void) {
86   FILE *fp = rtp_connect();
87
88   if(!fp)
89     return 0;                           /* no connection -> not running */
90   fclose(fp);                           /* connection -> running */
91   return 1;
92 }
93
94 /** @brief Activate the RTP player if it is not running */
95 void start_rtp(void) {
96   pid_t pid;
97   int w, fd;
98
99   if(rtp_running())
100     return;                             /* already running */
101   /* double-fork so we don't have to wait() later */
102   if(!(pid = xfork())) {
103     if(setsid() < 0)
104       fatal(errno, "error calling setsid");
105     if(!(pid = xfork())) {
106       /* grandchild */
107       exitfn = _exit;
108       /* log errors and output somewhere reasonably sane.  rtp_running()
109        * will have made sure the directory exists. */
110       if((fd = open(rtp_log, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
111         fatal(errno, "creating %s", rtp_log);
112       if(dup2(fd, 1) < 0
113          || dup2(fd, 2) < 0)
114         fatal(errno, "dup2");
115       if(close(fd) < 0)
116         fatal(errno, "close");
117       /* We don't want to hang onto whatever stdin was */
118       if((fd = open("/dev/null", O_RDONLY)) < 0)
119         fatal(errno, "opening /dev/null");
120       if(dup2(fd, 0) < 0)
121         fatal(errno, "dup2");
122       if(close(fd) < 0)
123         fatal(errno, "close");
124       /* execute the player */
125       execlp("disorder-playrtp",
126              "disorder-playrtp", "--socket", rtp_socket, (char *)0);
127       fatal(errno, "disorder-playrtp");
128     } else {
129       /* child */
130       _exit(0);
131     }
132   } else {
133     /* parent */
134     while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
135       ;
136   }
137 }
138
139 /** @brief Stop the RTP player if it is running */
140 void stop_rtp(void) {
141   FILE *fp = rtp_connect();
142
143   if(!fp)
144     return;                             /* already stopped */
145   fprintf(fp, "stop\n");
146   fclose(fp);
147 }
148
149 /*
150 Local Variables:
151 c-basic-offset:2
152 comment-column:40
153 fill-column:79
154 indent-tabs-mode:nil
155 End:
156 */