2 wmget - A background download manager as a Window Maker dock app
3 Copyright (c) 2001-2003 Aaron Trickey <aaron@amtrickey.net>
5 Permission is hereby granted, free of charge, to any person
6 obtaining a copy of this software and associated documentation files
7 (the "Software"), to deal in the Software without restriction,
8 including without limitation the rights to use, copy, modify, merge,
9 publish, distribute, sublicense, and/or sell copies of the Software,
10 and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 ********************************************************************
25 iq.c - Core functions implementing the server's Unix-domain socket
27 To submit jobs to the server's input queue, you connect to a Unix-
28 domain socket it opens. These functions are responsible for
29 setting up, connecting to, and accepting connections from that
30 socket, and are used by server.c and request.c.
39 #include <sys/types.h>
40 #include <sys/socket.h>
48 /* iqsun == the Unix-domain socket address, aka the pathname, of our
49 * socket. iqsun_len == the number of bytes in iqsun incl. the header
50 * and the pathname string, excluding the unused bytes after the
51 * string's terminator.
53 static struct sockaddr_un iqsun
;
54 static int iqsun_len
= 0; /* 0 => not yet initialized */
55 const char *iqname
= ".wmget.iq";
62 static int iq_init_address (void)
64 const char *homedir
= home_directory ();
66 if (iqsun_len
) /* already initialized */
69 /* Our pathname length is constrained by sizeof(sockaddr_un), and
70 * must be able to fit the homedir, the /, the iqname, and the \0.
72 if (strlen (homedir
) > sizeof iqsun
.sun_path
- 2 - sizeof iqname
) {
73 error ("Home directory path is too long, can't construct "
78 sprintf (iqsun
.sun_path
, "%s/%s", homedir
, iqname
);
80 debug ("IQ = '%s'", iqsun
.sun_path
);
82 iqsun
.sun_family
= AF_UNIX
;
84 iqsun_len
= sizeof iqsun
- sizeof iqsun
.sun_path
85 + strlen (iqsun
.sun_path
) + 1;
91 FILE *iq_client_connect (void)
96 if (iq_init_address ())
99 if ((fd
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
100 error_sys ("Could not create Unix-domain socket to talk to server");
104 if (connect (fd
, (struct sockaddr
*)&iqsun
, iqsun_len
) < 0) {
105 error_sys ("Could not connect to the server");
110 if (!(fp
= fdopen (fd
, "r+"))) {
111 /* this should never fail for any reparable reason */
112 error_sys ("fdopen");
120 /* Server listener socket */
121 static int iq_listen_fd
;
124 /** was going to use this in atexit(), but the problem is that we fork,
125 * and those children inherit the atexits, and well ...
126 static void iq_server_cleanup (void)
128 close (iq_listen_fd);
129 unlink (iqsun.sun_path);
134 int iq_server_init (void)
138 if (iq_init_address ())
142 if ((fd
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
143 error_sys ("Could not create Unix-domain socket to receive requests");
147 /* We're going to listen on this socket and don't want accept() to
150 if (fcntl (fd
, F_SETFL
, (long)O_NONBLOCK
) < 0) {
156 /* Before proceeding, try to *connect* to the iq. If this succeeds,
157 * then uh-oh, there's another wmget running out there.
159 test_fd
= connect (fd
, (struct sockaddr
*)&iqsun
, iqsun_len
);
163 "There's another wmget dock running. You can only run "
168 /* Good. Just in case the pathname exists, unlink it. */
169 unlink (iqsun
.sun_path
);
171 if (bind (fd
, (struct sockaddr
*)&iqsun
, iqsun_len
) < 0) {
177 /* Tighten up the new filename's permissions. */
178 if (chmod (iqsun
.sun_path
, S_IRWXU
) < 0) {
184 if (listen (fd
, 5) < 0) {
185 error_sys ("listen");
196 FILE *iq_server_accept (void)
200 struct sockaddr_un sun
;
201 int sun_len
= sizeof sun
;
203 if ((fd
= accept (iq_listen_fd
, (struct sockaddr
*)&sun
,
205 if (errno
== EAGAIN
) {
206 /* Simply no connections waiting. */
210 error_sys ("accept");
214 debug ("got a connection...");
216 if (!(fp
= fdopen (fd
, "r+"))) {
217 error_sys ("fdopen");
226 int iq_get_listen_fd (void)