/*
This file is part of GNUnet.
- Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2009-2013 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
}
+#ifndef WINDOWS
+/**
+ * If services crash, they can leave a unix domain socket file on the
+ * disk. This needs to be manually removed, because otherwise both
+ * bind() and connect() for the respective address will fail. In this
+ * function, we test if such a left-over file exists, and if so,
+ * remove it (unless there is a listening service at the address).
+ *
+ * @param un unix domain socket address to check
+ */
+void
+GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
+{
+ int s;
+ int eno;
+ struct stat sbuf;
+ int ret;
+
+ s = socket (AF_UNIX, SOCK_STREAM, 0);
+ if (-1 == s)
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+ "Failed to open AF_UNIX socket");
+ return;
+ }
+ ret = connect (s,
+ (struct sockaddr *) un,
+ sizeof (struct sockaddr_un));
+ eno = errno;
+ GNUNET_break (0 == close (s));
+ if (0 == ret)
+ return; /* another process is listening, do not remove! */
+ if (ECONNREFUSED != eno)
+ return; /* some other error, likely "no such file or directory" -- all well */
+ /* should unlink, but sanity checks first */
+ if (0 != stat (un->sun_path,
+ &sbuf))
+ return; /* failed to 'stat', likely does not exist after all */
+ if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
+ return; /* refuse to unlink anything except sockets */
+ /* finally, really unlink */
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ "Removing left-over `%s' from previous exeuction\n",
+ un->sun_path);
+ if (0 != unlink (un->sun_path))
+ GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+ "unlink",
+ un->sun_path);
+}
+#endif
+
+
+
#ifndef FD_COPY
-#define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
+#define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)
#endif
#endif
#endif
#ifndef WINDOWS
+ if (AF_UNIX == address->sa_family)
+ GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
{
const int on = 1;
LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
"setsockopt");
}
-#endif
-#ifndef WINDOWS
{
/* set permissions of newly created non-abstract UNIX domain socket to
"user-only"; applications can choose to relax this later */
old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
#endif
- ret = bind (desc->fd, address, address_len);
+ ret = bind (desc->fd,
+ address,
+ address_len);
+
#ifndef WINDOWS
if (not_abstract)
(void) umask (old_mask);
if (SOCKET_ERROR == ret)
SetErrnoFromWinsockError (WSAGetLastError ());
#endif
- if (ret != 0)
+ if (0 != ret)
return GNUNET_SYSERR;
#ifndef MINGW
desc->addr = GNUNET_malloc (address_len);
- memcpy (desc->addr, address, address_len);
+ GNUNET_memcpy (desc->addr, address, address_len);
desc->addrlen = address_len;
#endif
return GNUNET_OK;
GNUNET_array_grow (to->handles,
to->handles_size,
from->handles_pos * 2);
- memcpy (to->handles,
+ GNUNET_memcpy (to->handles,
from->handles,
from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
to->handles_pos = from->handles_pos;
* @return #GNUNET_YES if the pipe is ready for reading
*/
static int
-pipe_read_ready (struct GNUNET_DISK_FileHandle *fh)
+pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
{
DWORD error;
BOOL bret;
* @return #GNUNET_YES if the pipe is having an IO exception.
*/
static int
-pipe_except_ready (struct GNUNET_DISK_FileHandle *fh)
+pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
{
DWORD dwBytes;
int except,
HANDLE set_for_sure)
{
- struct GNUNET_DISK_FileHandle *fh;
+ const struct GNUNET_DISK_FileHandle *fh;
unsigned int roff;
unsigned int woff;
}
else
{
- if (WAIT_OBJECT_0 == WaitForSingleObject (fh, 0))
+ if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0))
fds->handles[woff++] = fh;
}
}
struct GNUNET_NETWORK_FDSet *efds,
const struct GNUNET_TIME_Relative timeout)
{
- struct GNUNET_DISK_FileHandle *fh;
+ const struct GNUNET_DISK_FileHandle *fh;
int nfds;
int handles;
unsigned int i;