static PyObject *journal_sendv(PyObject *self, PyObject *args) { struct iovec *iov = NULL; int argc; int i, r; PyObject *ret = NULL; PyObject **encoded; /* Allocate an array for the argument strings */ argc = PyTuple_Size(args); encoded = alloca0(argc * sizeof(PyObject*)); /* Allocate sufficient iovector space for the arguments. */ iov = alloca(argc * sizeof(struct iovec)); /* Iterate through the Python arguments and fill the iovector. */ for (i = 0; i < argc; ++i) { PyObject *item = PyTuple_GetItem(args, i); char *stritem; Py_ssize_t length; if (PyUnicode_Check(item)) { encoded[i] = PyUnicode_AsEncodedString(item, "utf-8", "strict"); if (encoded[i] == NULL) goto out; item = encoded[i]; } if (PyBytes_AsStringAndSize(item, &stritem, &length)) goto out; iov[i].iov_base = stritem; iov[i].iov_len = length; } /* Send the iovector to the journal. */ r = sd_journal_sendv(iov, argc); if (r < 0) { errno = -r; PyErr_SetFromErrno(PyExc_IOError); goto out; } /* End with success. */ Py_INCREF(Py_None); ret = Py_None; out: for (i = 0; i < argc; ++i) Py_XDECREF(encoded[i]); return ret; }
out: if (xattr) dict_unref (xattr); return ret; } int afr_selfheal_locked_metadata_inspect (call_frame_t *frame, xlator_t *this, inode_t *inode, gf_boolean_t *metadata_selfheal) { int ret = -1; unsigned char *locked_on = NULL; afr_private_t *priv = this->private; locked_on = alloca0 (priv->child_count); ret = afr_selfheal_inodelk (frame, this, inode, this->name, LLONG_MAX - 1, 0, locked_on); { if (ret == 0) { /* Not a single lock */ ret = -afr_final_errno (frame->local, priv); if (ret == 0) ret = -ENOTCONN;/* all invalid responses */ goto out; } ret = afr_selfheal_unlocked_inspect (frame, this, inode->gfid, NULL, NULL, metadata_selfheal, NULL); }
unsigned char *readable = NULL; afr_local_t *local = NULL; afr_private_t *priv = NULL; struct afr_reply *replies = NULL; int i = 0; int ret = 0; quota_meta_t size = {0, }; quota_meta_t max_size = {0, }; int readable_cnt = 0; int read_subvol = -1; local = frame->local; priv = this->private; replies = local->replies; readable = alloca0 (priv->child_count); afr_inode_read_subvol_get (local->inode, this, readable, 0, 0); readable_cnt = AFR_COUNT (readable, priv->child_count); for (i = 0; i < priv->child_count; i++) { if (!replies[i].valid || replies[i].op_ret == -1) continue; if (readable_cnt && !readable[i]) continue; if (!replies[i].xdata) continue; ret = quota_dict_get_meta (replies[i].xdata, QUOTA_SIZE_KEY, &size); if (ret == -1)
int ethtool_get_driver(int *fd, const char *ifname, char **ret) { struct ethtool_drvinfo ecmd = { .cmd = ETHTOOL_GDRVINFO }; struct ifreq ifr = { .ifr_data = (void*) &ecmd }; char *d; int r; if (*fd < 0) { r = ethtool_connect(fd); if (r < 0) return log_warning_errno(r, "link_config: could not connect to ethtool: %m"); } strscpy(ifr.ifr_name, IFNAMSIZ, ifname); r = ioctl(*fd, SIOCETHTOOL, &ifr); if (r < 0) return -errno; d = strdup(ecmd.driver); if (!d) return -ENOMEM; *ret = d; return 0; } int ethtool_set_speed(int *fd, const char *ifname, unsigned int speed, Duplex duplex) { struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET }; struct ifreq ifr = { .ifr_data = (void*) &ecmd }; bool need_update = false; int r; if (speed == 0 && duplex == _DUP_INVALID) return 0; if (*fd < 0) { r = ethtool_connect(fd); if (r < 0) return log_warning_errno(r, "link_config: could not connect to ethtool: %m"); } strscpy(ifr.ifr_name, IFNAMSIZ, ifname); r = ioctl(*fd, SIOCETHTOOL, &ifr); if (r < 0) return -errno; if (ethtool_cmd_speed(&ecmd) != speed) { ethtool_cmd_speed_set(&ecmd, speed); need_update = true; } switch (duplex) { case DUP_HALF: if (ecmd.duplex != DUPLEX_HALF) { ecmd.duplex = DUPLEX_HALF; need_update = true; } break; case DUP_FULL: if (ecmd.duplex != DUPLEX_FULL) { ecmd.duplex = DUPLEX_FULL; need_update = true; } break; default: break; } if (need_update) { ecmd.cmd = ETHTOOL_SSET; r = ioctl(*fd, SIOCETHTOOL, &ifr); if (r < 0) return -errno; } return 0; } int ethtool_set_wol(int *fd, const char *ifname, WakeOnLan wol) { struct ethtool_wolinfo ecmd = { .cmd = ETHTOOL_GWOL }; struct ifreq ifr = { .ifr_data = (void*) &ecmd }; bool need_update = false; int r; if (wol == _WOL_INVALID) return 0; if (*fd < 0) { r = ethtool_connect(fd); if (r < 0) return log_warning_errno(r, "link_config: could not connect to ethtool: %m"); } strscpy(ifr.ifr_name, IFNAMSIZ, ifname); r = ioctl(*fd, SIOCETHTOOL, &ifr); if (r < 0) return -errno; switch (wol) { case WOL_PHY: if (ecmd.wolopts != WAKE_PHY) { ecmd.wolopts = WAKE_PHY; need_update = true; } break; case WOL_UCAST: if (ecmd.wolopts != WAKE_UCAST) { ecmd.wolopts = WAKE_UCAST; need_update = true; } break; case WOL_MCAST: if (ecmd.wolopts != WAKE_MCAST) { ecmd.wolopts = WAKE_MCAST; need_update = true; } break; case WOL_BCAST: if (ecmd.wolopts != WAKE_BCAST) { ecmd.wolopts = WAKE_BCAST; need_update = true; } break; case WOL_ARP: if (ecmd.wolopts != WAKE_ARP) { ecmd.wolopts = WAKE_ARP; need_update = true; } break; case WOL_MAGIC: if (ecmd.wolopts != WAKE_MAGIC) { ecmd.wolopts = WAKE_MAGIC; need_update = true; } break; case WOL_MAGICSECURE: if (ecmd.wolopts != WAKE_MAGICSECURE) { ecmd.wolopts = WAKE_MAGICSECURE; need_update = true; } break; case WOL_OFF: if (ecmd.wolopts != 0) { ecmd.wolopts = 0; need_update = true; } break; default: break; } if (need_update) { ecmd.cmd = ETHTOOL_SWOL; r = ioctl(*fd, SIOCETHTOOL, &ifr); if (r < 0) return -errno; } return 0; } static int get_stringset(int fd, struct ifreq *ifr, int stringset_id, struct ethtool_gstrings **gstrings) { _cleanup_free_ struct ethtool_gstrings *strings = NULL; struct { struct ethtool_sset_info info; uint32_t space; } buffer = { .info = { .cmd = ETHTOOL_GSSET_INFO, .sset_mask = UINT64_C(1) << stringset_id, }, }; unsigned len; int r; ifr->ifr_data = (void *) &buffer.info; r = ioctl(fd, SIOCETHTOOL, ifr); if (r < 0) return -errno; if (!buffer.info.sset_mask) return -EINVAL; len = buffer.info.data[0]; strings = malloc0(sizeof(struct ethtool_gstrings) + len * ETH_GSTRING_LEN); if (!strings) return -ENOMEM; strings->cmd = ETHTOOL_GSTRINGS; strings->string_set = stringset_id; strings->len = len; ifr->ifr_data = (void *) strings; r = ioctl(fd, SIOCETHTOOL, ifr); if (r < 0) return -errno; *gstrings = TAKE_PTR(strings); return 0; } static int find_feature_index(struct ethtool_gstrings *strings, const char *feature) { unsigned i; for (i = 0; i < strings->len; i++) { if (streq((char *) &strings->data[i * ETH_GSTRING_LEN], feature)) return i; } return -1; } int ethtool_set_features(int *fd, const char *ifname, int *features) { _cleanup_free_ struct ethtool_gstrings *strings = NULL; struct ethtool_sfeatures *sfeatures; int block, bit, i, r; struct ifreq ifr = {}; if (*fd < 0) { r = ethtool_connect(fd); if (r < 0) return log_warning_errno(r, "link_config: could not connect to ethtool: %m"); } strscpy(ifr.ifr_name, IFNAMSIZ, ifname); r = get_stringset(*fd, &ifr, ETH_SS_FEATURES, &strings); if (r < 0) return log_warning_errno(r, "link_config: could not get ethtool features for %s", ifname); sfeatures = alloca0(sizeof(struct ethtool_sfeatures) + DIV_ROUND_UP(strings->len, 32U) * sizeof(sfeatures->features[0])); sfeatures->cmd = ETHTOOL_SFEATURES; sfeatures->size = DIV_ROUND_UP(strings->len, 32U); for (i = 0; i < _NET_DEV_FEAT_MAX; i++) { if (features[i] != -1) { r = find_feature_index(strings, netdev_feature_table[i]); if (r < 0) { log_warning_errno(r, "link_config: could not find feature: %s", netdev_feature_table[i]); continue; } block = r / 32; bit = r % 32; sfeatures->features[block].valid |= 1 << bit; if (features[i]) sfeatures->features[block].requested |= 1 << bit; else sfeatures->features[block].requested &= ~(1 << bit); } } ifr.ifr_data = (void *) sfeatures; r = ioctl(*fd, SIOCETHTOOL, &ifr); if (r < 0) return log_warning_errno(r, "link_config: could not set ethtool features for %s", ifname); return 0; }
int server_open_stdout_socket(Server *s) { static const union sockaddr_union sa = { .un.sun_family = AF_UNIX, .un.sun_path = "/run/systemd/journal/stdout", }; int r; assert(s); if (s->stdout_fd < 0) { s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); if (s->stdout_fd < 0) return log_error_errno(errno, "socket() failed: %m"); (void) unlink(sa.un.sun_path); r = bind(s->stdout_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); if (r < 0) return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); (void) chmod(sa.un.sun_path, 0666); if (listen(s->stdout_fd, SOMAXCONN) < 0) return log_error_errno(errno, "listen(%s) failed: %m", sa.un.sun_path); } else fd_nonblock(s->stdout_fd, 1); r = sd_event_add_io(s->event, &s->stdout_event_source, s->stdout_fd, EPOLLIN, stdout_stream_new, s); if (r < 0) return log_error_errno(r, "Failed to add stdout server fd to event source: %m"); r = sd_event_source_set_priority(s->stdout_event_source, SD_EVENT_PRIORITY_NORMAL+5); if (r < 0) return log_error_errno(r, "Failed to adjust priority of stdout server event source: %m"); return 0; } void stdout_stream_send_notify(StdoutStream *s) { struct iovec iovec = { .iov_base = (char*) "FDSTORE=1", .iov_len = strlen("FDSTORE=1"), }; struct msghdr msghdr = { .msg_iov = &iovec, .msg_iovlen = 1, }; struct cmsghdr *cmsg; ssize_t l; assert(s); assert(!s->fdstore); assert(s->in_notify_queue); assert(s->server); assert(s->server->notify_fd >= 0); /* Store the connection fd in PID 1, so that we get it passed * in again on next start */ msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); msghdr.msg_control = alloca0(msghdr.msg_controllen); cmsg = CMSG_FIRSTHDR(&msghdr); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(sizeof(int)); memcpy(CMSG_DATA(cmsg), &s->fd, sizeof(int)); l = sendmsg(s->server->notify_fd, &msghdr, MSG_DONTWAIT|MSG_NOSIGNAL); if (l < 0) { if (errno == EAGAIN) return; log_error_errno(errno, "Failed to send stream file descriptor to service manager: %m"); } else { log_debug("Successfully sent stream file descriptor to service manager."); s->fdstore = 1; } LIST_REMOVE(stdout_stream_notify_queue, s->server->stdout_streams_notify_queue, s); s->in_notify_queue = false; }