chiark / gitweb /
remove redundant color
[disorder] / disobedience / rtp.c
CommitLineData
a99c4e9a
RK
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"
9fbe0996 25#include <sys/types.h>
a99c4e9a
RK
26#include <sys/stat.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <sys/un.h>
c764e004 30#include <sys/utsname.h>
9fbe0996 31#include <sys/wait.h>
a99c4e9a
RK
32
33/** @brief Path to RTP player's control socket */
34static char *rtp_socket;
35
36/** @brief Path to RTP player's logfile */
37static char *rtp_log;
38
39/** @brief Initialize @ref rtp_socket and @ref rtp_log if necessary */
40static void rtp_init(void) {
41 if(!rtp_socket) {
42 const char *home = getenv("HOME");
c764e004
RK
43 char *dir, *base;
44 struct utsname uts;
a99c4e9a 45
c764e004 46 byte_xasprintf(&dir, "%s/.disorder/", home);
a99c4e9a 47 mkdir(dir, 02700);
c764e004
RK
48 if(uname(&uts) < 0)
49 byte_xasprintf(&base, "%s/", dir);
50 else
51 byte_xasprintf(&base, "%s/%s-", dir, uts.nodename);
52 byte_xasprintf(&rtp_socket, "%srtp", base);
53 byte_xasprintf(&rtp_log, "%srtp.log", base);
a99c4e9a
RK
54 }
55}
56
57/** @brief Return a connection to the RTP player's control socket */
58static FILE *rtp_connect(void) {
59 struct sockaddr_un un;
60 int fd;
61 FILE *fp;
62
63 rtp_init();
64 memset(&un, 0, sizeof un);
65 un.sun_family = AF_UNIX;
66 strcpy(un.sun_path, rtp_socket);
67 fd = xsocket(PF_UNIX, SOCK_STREAM, 0);
68 if(connect(fd, (const struct sockaddr *)&un, sizeof un) < 0) {
69 /* Connection refused just means that the player quit without deleting its
70 * socket */
71 switch(errno) {
72 case ECONNREFUSED:
73 case ENOENT:
74 break;
75 default:
76 /* Anything else may be a problem */
77 fpopup_msg(GTK_MESSAGE_ERROR, "connecting to %s: %s",
78 rtp_socket, strerror(errno));
79 break;
80 }
81 close(fd);
82 return 0;
83 }
84 if(!(fp = fdopen(fd, "r+"))) {
85 fpopup_msg(GTK_MESSAGE_ERROR, "error calling fdopen: %s", strerror(errno));
86 close(fd);
87 return 0;
88 }
89 return fp;
90}
91
92/** @brief Return non-0 iff the RTP player is running */
93int rtp_running(void) {
94 FILE *fp = rtp_connect();
95
96 if(!fp)
97 return 0; /* no connection -> not running */
98 fclose(fp); /* connection -> running */
99 return 1;
100}
101
102/** @brief Activate the RTP player if it is not running */
103void start_rtp(void) {
104 pid_t pid;
105 int w, fd;
106
107 if(rtp_running())
108 return; /* already running */
109 /* double-fork so we don't have to wait() later */
110 if(!(pid = xfork())) {
111 if(setsid() < 0)
112 fatal(errno, "error calling setsid");
113 if(!(pid = xfork())) {
114 /* grandchild */
115 exitfn = _exit;
116 /* log errors and output somewhere reasonably sane. rtp_running()
117 * will have made sure the directory exists. */
118 if((fd = open(rtp_log, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
119 fatal(errno, "creating %s", rtp_log);
120 if(dup2(fd, 1) < 0
121 || dup2(fd, 2) < 0)
122 fatal(errno, "dup2");
123 if(close(fd) < 0)
124 fatal(errno, "close");
125 /* We don't want to hang onto whatever stdin was */
126 if((fd = open("/dev/null", O_RDONLY)) < 0)
127 fatal(errno, "opening /dev/null");
128 if(dup2(fd, 0) < 0)
129 fatal(errno, "dup2");
130 if(close(fd) < 0)
131 fatal(errno, "close");
132 /* execute the player */
133 execlp("disorder-playrtp",
134 "disorder-playrtp", "--socket", rtp_socket, (char *)0);
135 fatal(errno, "disorder-playrtp");
136 } else {
137 /* child */
138 _exit(0);
139 }
140 } else {
141 /* parent */
142 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
143 ;
144 }
145}
146
147/** @brief Stop the RTP player if it is running */
148void stop_rtp(void) {
149 FILE *fp = rtp_connect();
150
151 if(!fp)
152 return; /* already stopped */
153 fprintf(fp, "stop\n");
154 fclose(fp);
155}
156
157/*
158Local Variables:
159c-basic-offset:2
160comment-column:40
161fill-column:79
162indent-tabs-mode:nil
163End:
164*/