1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-cleanup-sockets.c dbus-cleanup-sockets utility
4 * Copyright (C) 2003 Red Hat, Inc.
5 * Copyright (C) 2002 Michael Meeks
7 * Note that this file is NOT licensed under the Academic Free License,
8 * as it is based on linc-cleanup-sockets which is LGPL.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
32 #include <sys/socket.h>
47 #define NULL ((void*) 0)
51 xmalloc (size_t bytes)
62 fprintf (stderr, "Allocation of %d bytes failed\n",
71 xrealloc (void *old, size_t bytes)
81 mem = realloc (old, bytes);
85 fprintf (stderr, "Reallocation of %d bytes failed\n",
98 SOCKET_FAILED_TO_HANDLE,
104 static int alive_count = 0;
105 static int cleaned_count = 0;
106 static int unhandled_count = 0;
117 socket_entry_new (const char *dir,
123 se = xmalloc (sizeof (SocketEntry));
125 len = strlen (dir) + strlen (fname) + 2; /* 2 = nul and '/' */
126 se->name = xmalloc (len);
128 strcpy (se->name, dir);
129 strcat (se->name, "/");
130 strcat (se->name, fname);
134 se->status = SOCKET_UNKNOWN;
143 free_socket_entry (SocketEntry *se)
153 read_sockets (const char *dir,
154 SocketEntry ***entries_p,
159 SocketEntry **entries;
165 entries = xmalloc (sizeof (SocketEntry*) * allocated);
167 dirh = opendir (dir);
170 fprintf (stderr, "Failed to open directory %s: %s\n",
171 dir, strerror (errno));
175 while ((dent = readdir (dirh)))
179 if (strncmp (dent->d_name, "dbus-", 5) != 0)
182 se = socket_entry_new (dir, dent->d_name);
184 if (n_entries == allocated)
187 entries = xrealloc (entries, sizeof (SocketEntry*) * allocated);
190 entries[n_entries] = se;
196 *entries_p = entries;
197 *n_entries_p = n_entries;
201 open_socket (SocketEntry *se)
204 struct sockaddr_un saddr;
206 if (se->n_retries > 5)
208 fprintf (stderr, "Warning: giving up on socket %s after several retries; unable to determine socket's status\n",
210 return SOCKET_FAILED_TO_HANDLE;
215 se->fd = socket (AF_UNIX, SOCK_STREAM, 0);
218 fprintf (stderr, "Warning: failed to open a socket to use for connecting: %s\n",
220 return SOCKET_UNKNOWN;
223 if (fcntl (se->fd, F_SETFL, O_NONBLOCK) < 0)
225 fprintf (stderr, "Warning: failed set socket %s nonblocking: %s\n",
226 se->name, strerror (errno));
227 return SOCKET_UNKNOWN;
231 memset (&saddr, '\0', sizeof (saddr)); /* nul-terminates the sun_path */
233 saddr.sun_family = AF_UNIX;
234 strncpy (saddr.sun_path, se->name, sizeof (saddr.sun_path) - 1);
238 ret = connect (se->fd, (struct sockaddr*) &saddr, sizeof (saddr));
240 while (ret < 0 && errno == EINTR);
250 return SOCKET_UNKNOWN;
254 fprintf (stderr, "Warning: unexpected error connecting to socket %s: %s\n",
255 se->name, strerror (errno));
256 return SOCKET_FAILED_TO_HANDLE;
262 handle_sockets (SocketEntry **entries,
271 while (i < n_entries)
281 fprintf (stderr, "Internal error, socket has fd kept open while status = %d\n",
286 if (se->status != SOCKET_UNKNOWN)
289 status = open_socket (se);
295 if (unlink (se->name) < 0)
297 fprintf (stderr, "Warning: Failed to delete %s: %s\n",
298 se->name, strerror (errno));
300 se->status = SOCKET_FAILED_TO_HANDLE;
303 se->status = SOCKET_UNLINKED;
310 case SOCKET_FAILED_TO_HANDLE:
315 case SOCKET_UNLINKED:
316 fprintf (stderr, "Bad status from open_socket(), should not happen\n");
327 if (se->status == SOCKET_UNKNOWN)
331 return n_unknown == 0;
335 clean_dir (const char *dir)
337 SocketEntry **entries;
340 read_sockets (dir, &entries, &n_entries);
342 /* open_socket() will fail conclusively after
343 * several retries, so this loop is guaranteed
344 * to terminate eventually
346 while (!handle_sockets (entries, n_entries))
348 fprintf (stderr, "Unable to determine state of some sockets, retrying in 2 seconds\n");
352 unhandled_count += (n_entries - alive_count - cleaned_count);
360 fprintf (stderr, "dbus-cleanup-sockets [--version] [--help] <socketdir>\n");
367 printf ("D-Bus Socket Cleanup Utility %s\n"
368 "Copyright (C) 2003 Red Hat, Inc.\n"
369 "Copyright (C) 2002 Michael Meeks\n"
370 "This is free software; see the source for copying conditions.\n"
371 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
377 main (int argc, char **argv)
383 saw_doubledash = FALSE;
388 const char *arg = argv[i];
390 if (strcmp (arg, "--help") == 0 ||
391 strcmp (arg, "-h") == 0 ||
392 strcmp (arg, "-?") == 0)
394 else if (strcmp (arg, "--version") == 0)
396 else if (!saw_doubledash)
398 if (strcmp (arg, "--") == 0)
399 saw_doubledash = TRUE;
400 else if (*arg == '-')
407 fprintf (stderr, "dbus-cleanup-sockets only supports a single directory name\n");
417 /* Default to session socket dir, usually /tmp */
419 dirname = DBUS_SESSION_SOCKET_DIR;
424 printf ("Cleaned up %d sockets in %s; %d sockets are still in use; %d in unknown state\n",
425 cleaned_count, dirname, alive_count, unhandled_count);
427 printf ("This system does not support UNIX domain sockets, so dbus-cleanup-sockets does nothing\n");