wmclockmon: drop `getuid()` error checks
[dockapps.git] / wmget / iq.c
blob985640e4c28aa8bb92737f8deb9448ced6287e38
1 /*
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.
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <sys/poll.h>
43 #include <sys/stat.h>
45 #include "wmget.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";
57 int init_paths (void)
59 return 0;
62 static int iq_init_address (void)
64 const char *homedir = home_directory ();
66 if (iqsun_len) /* already initialized */
67 return 0;
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 "
74 "socket name");
75 return 1;
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;
87 return 0;
91 FILE *iq_client_connect (void)
93 int fd;
94 FILE *fp;
96 if (iq_init_address ())
97 return 0;
99 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
100 error_sys ("Could not create Unix-domain socket to talk to server");
101 return 0;
104 if (connect (fd, (struct sockaddr *)&iqsun, iqsun_len) < 0) {
105 error_sys ("Could not connect to the server");
106 close (fd);
107 return 0;
110 if (!(fp = fdopen (fd, "r+"))) {
111 /* this should never fail for any reparable reason */
112 error_sys ("fdopen");
113 close (fd);
114 return 0;
117 return fp;
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)
136 int fd, test_fd;
138 if (iq_init_address ())
139 return 1;
142 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
143 error_sys ("Could not create Unix-domain socket to receive requests");
144 return 1;
147 /* We're going to listen on this socket and don't want accept() to
148 * block
150 if (fcntl (fd, F_SETFL, (long)O_NONBLOCK) < 0) {
151 error_sys ("fcntl");
152 close (fd);
153 return 1;
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);
160 if (test_fd >= 0) {
161 close (test_fd);
162 error (
163 "There's another wmget dock running. You can only run "
164 "one at a time.");
165 return 1;
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) {
172 error_sys ("bind");
173 close (fd);
174 return 1;
177 /* Tighten up the new filename's permissions. */
178 if (chmod (iqsun.sun_path, S_IRWXU) < 0) {
179 error_sys ("chmod");
180 close (fd);
181 return 1;
184 if (listen (fd, 5) < 0) {
185 error_sys ("listen");
186 close (fd);
187 return 1;
190 iq_listen_fd = fd;
192 return 0;
196 FILE *iq_server_accept (void)
198 int fd;
199 FILE *fp;
200 struct sockaddr_un sun;
201 int sun_len = sizeof sun;
203 if ((fd = accept (iq_listen_fd, (struct sockaddr *)&sun,
204 &sun_len)) < 0) {
205 if (errno == EAGAIN) {
206 /* Simply no connections waiting. */
207 return 0;
210 error_sys ("accept");
211 return 0;
214 debug ("got a connection...");
216 if (!(fp = fdopen (fd, "r+"))) {
217 error_sys ("fdopen");
218 close (fd);
219 return 0;
222 return fp;
226 int iq_get_listen_fd (void)
228 return iq_listen_fd;