269
/* Drain all the data currently in the receive queue of a datagram socket (and
270
* possibly additional data). There is no way to know how many packets are in
271
* the receive queue, but we do know that the total number of bytes queued does
272
* not exceed the receive buffer size, so we pull packets until none are left
273
* or we've read that many bytes. */
279
rcvbuf = get_socket_rcvbuf(fd);
285
/* In Linux, specifying MSG_TRUNC in the flags argument causes the
286
* datagram length to be returned, even if that is longer than the
287
* buffer provided. Thus, we can use a 1-byte buffer to discard the
288
* incoming datagram and still be able to account how many bytes were
289
* removed from the receive buffer.
291
* On other Unix-like OSes, MSG_TRUNC has no effect in the flags
293
char buffer[LINUX ? 1 : 2048];
294
ssize_t n_bytes = recv(fd, buffer, sizeof buffer,
295
MSG_TRUNC | MSG_DONTWAIT);
296
if (n_bytes <= 0 || n_bytes >= rcvbuf) {
305
268
/* Returns the size of socket 'sock''s receive buffer (SO_RCVBUF), or a
306
269
* negative errno value if an error occurs. */
336
/* Attempts to shorten 'name' by opening a file descriptor for the directory
337
* part of the name and indirecting through /proc/self/fd/<dirfd>/<basename>.
338
* On systems with Linux-like /proc, this works as long as <basename> isn't too
341
* On success, returns 0 and stores the short name in 'short_name' and a
342
* directory file descriptor to eventually be closed in '*dirfpd'. */
344
shorten_name_via_proc(const char *name, char short_name[MAX_UN_LEN + 1],
355
dir = dir_name(name);
356
dirfd = open(dir, O_DIRECTORY | O_RDONLY);
358
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
361
VLOG_WARN_RL(&rl, "%s: open failed (%s)", dir, ovs_strerror(error));
368
base = base_name(name);
369
len = snprintf(short_name, MAX_UN_LEN + 1,
370
"/proc/self/fd/%d/%s", dirfd, base);
373
if (len >= 0 && len <= MAX_UN_LEN) {
382
/* Attempts to shorten 'name' by creating a symlink for the directory part of
383
* the name and indirecting through <symlink>/<basename>. This works on
384
* systems that support symlinks, as long as <basename> isn't too long.
386
* On success, returns 0 and stores the short name in 'short_name' and the
387
* symbolic link to eventually delete in 'linkname'. */
389
shorten_name_via_symlink(const char *name, char short_name[MAX_UN_LEN + 1],
390
char linkname[MAX_UN_LEN + 1])
392
char *abs, *dir, *base;
397
abs = abs_file_name(NULL, name);
399
base = base_name(abs);
402
tmpdir = getenv("TMPDIR");
403
if (tmpdir == NULL) {
407
for (i = 0; i < 1000; i++) {
410
len = snprintf(linkname, MAX_UN_LEN + 1,
411
"%s/ovs-un-c-%"PRIu32, tmpdir, random_uint32());
412
error = (len < 0 || len > MAX_UN_LEN ? ENAMETOOLONG
413
: symlink(dir, linkname) ? errno
415
if (error != EEXIST) {
423
fatal_signal_add_file_to_unlink(linkname);
425
len = snprintf(short_name, MAX_UN_LEN + 1, "%s/%s", linkname, base);
426
if (len < 0 || len > MAX_UN_LEN) {
427
fatal_signal_unlink_file_now(linkname);
428
error = ENAMETOOLONG;
441
/* Stores in '*un' a sockaddr_un that refers to file 'name'. Stores in
442
* '*un_len' the size of the sockaddr_un.
444
* Returns 0 on success, otherwise a positive errno value.
446
* Uses '*dirfdp' and 'linkname' to store references to data when the caller no
447
* longer needs to use 'un'. On success, freeing these references with
448
* free_sockaddr_un() is mandatory to avoid a leak; on failure, freeing them is
449
* unnecessary but harmless. */
451
make_sockaddr_un(const char *name, struct sockaddr_un *un, socklen_t *un_len,
452
int *dirfdp, char linkname[MAX_UN_LEN + 1])
454
char short_name[MAX_UN_LEN + 1];
458
if (strlen(name) > MAX_UN_LEN) {
459
/* 'name' is too long to fit in a sockaddr_un. Try a workaround. */
460
int error = shorten_name_via_proc(name, short_name, dirfdp);
461
if (error == ENAMETOOLONG) {
462
error = shorten_name_via_symlink(name, short_name, linkname);
465
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
467
VLOG_WARN_RL(&rl, "Unix socket name %s is longer than maximum "
468
"%"PRIuSIZE" bytes", name, MAX_UN_LEN);
475
un->sun_family = AF_UNIX;
476
ovs_strzcpy(un->sun_path, name, sizeof un->sun_path);
477
*un_len = (offsetof(struct sockaddr_un, sun_path)
478
+ strlen (un->sun_path) + 1);
482
/* Clean up after make_sockaddr_un(). */
484
free_sockaddr_un(int dirfd, const char *linkname)
490
fatal_signal_unlink_file_now(linkname);
494
/* Binds Unix domain socket 'fd' to a file with permissions 0700. */
496
bind_unix_socket(int fd, struct sockaddr *sun, socklen_t sun_len)
498
/* According to _Unix Network Programming_, umask should affect bind(). */
499
mode_t old_umask = umask(0077);
500
int error = bind(fd, sun, sun_len) ? errno : 0;
505
/* Creates a Unix domain socket in the given 'style' (either SOCK_DGRAM or
506
* SOCK_STREAM) that is bound to '*bind_path' (if 'bind_path' is non-null) and
507
* connected to '*connect_path' (if 'connect_path' is non-null). If 'nonblock'
508
* is true, the socket is made non-blocking.
510
* Returns the socket's fd if successful, otherwise a negative errno value. */
512
make_unix_socket(int style, bool nonblock,
513
const char *bind_path, const char *connect_path)
518
fd = socket(PF_UNIX, style, 0);
523
/* Set nonblocking mode right away, if we want it. This prevents blocking
524
* in connect(), if connect_path != NULL. (In turn, that's a corner case:
525
* it will only happen if style is SOCK_STREAM or SOCK_SEQPACKET, and only
526
* if a backlog of un-accepted connections has built up in the kernel.) */
528
error = set_nonblocking(fd);
535
char linkname[MAX_UN_LEN + 1];
536
struct sockaddr_un un;
540
if (unlink(bind_path) && errno != ENOENT) {
541
VLOG_WARN("unlinking \"%s\": %s\n",
542
bind_path, ovs_strerror(errno));
544
fatal_signal_add_file_to_unlink(bind_path);
546
error = make_sockaddr_un(bind_path, &un, &un_len, &dirfd, linkname);
548
error = bind_unix_socket(fd, (struct sockaddr *) &un, un_len);
550
free_sockaddr_un(dirfd, linkname);
558
char linkname[MAX_UN_LEN + 1];
559
struct sockaddr_un un;
563
error = make_sockaddr_un(connect_path, &un, &un_len, &dirfd, linkname);
565
&& connect(fd, (struct sockaddr*) &un, un_len)
566
&& errno != EINPROGRESS) {
569
free_sockaddr_un(dirfd, linkname);
579
if (error == EAGAIN) {
583
fatal_signal_unlink_file_now(bind_path);
590
get_unix_name_len(socklen_t sun_len)
592
return (sun_len >= offsetof(struct sockaddr_un, sun_path)
593
? sun_len - offsetof(struct sockaddr_un, sun_path)
599
299
guess_netmask(ovs_be32 ip_)
1229
915
return ds_steal_cstr(&string);
1233
/* Calls ioctl() on an AF_INET sock, passing the specified 'command' and
1234
* 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1236
af_inet_ioctl(unsigned long int command, const void *arg)
1238
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1241
if (ovsthread_once_start(&once)) {
1242
sock = socket(AF_INET, SOCK_DGRAM, 0);
1244
int error = sock_errno();
1245
VLOG_ERR("failed to create inet socket: %s", sock_strerror(error));
1248
ovsthread_once_done(&once);
1251
return (sock < 0 ? -sock
1252
: ioctl(sock, command, arg) == -1 ? errno
1257
af_inet_ifreq_ioctl(const char *name, struct ifreq *ifr, unsigned long int cmd,
1258
const char *cmd_name)
1262
ovs_strzcpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
1263
error = af_inet_ioctl(cmd, ifr);
1265
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1266
VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
1267
ovs_strerror(error));
1273
919
/* sockaddr_storage helpers. */