static ssize_t sysfs_gpio_value_write(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) { struct gpio* gp; struct board* b; char a[ARG_LEN_MAX]; int ret; gp = dev_get_drvdata(dev); b = gp->brd; dbg(b->ch.debug,""); b->k.lock(&b->k); if ( gp->mode == GPIO_MODE_DIGITALO ) { if ( b->k.write8(&b->k, CMD_DIGITAL_WRITE | (*buf - '0') ) ) goto KER; if ( b->k.write8(&b->k, gp->io - gp->offset) ) goto KER; ret = b->k.read8(&b->k); } else if ( gp->mode == GPIO_MODE_ANALOGO || gp->mode == GPIO_MODE_PWM ) { if ( karg_normalize(a,ARG_LEN_MAX,buf,count) ) goto KER; ret = katoi(a); if ( b->k.write8(&b->k, CMD_ANALOG_WRITE ) ) goto KER; if ( b->k.write8(&b->k, gp->io - gp->offset) ) goto KER; if ( b->k.write8(&b->k, ret) ) goto KER; ret = b->k.read8(&b->k); } else { b->k.unlock(&b->k); return -EBADFD; } b->k.unlock(&b->k); return (ret == ERR_OK) ? count : -ECOMM; KER: dbg(b->ch.debug,"KER"); b->k.unlock(&b->k); return -ECOMM; }
static int socket_connect(struct file *f, const struct sockaddr *addr, size_t addrlen) { struct socket_file *socket = (struct socket_file *)f; struct sockaddr_storage addr_storage; int addr_storage_len; if (socket->shared->af == LINUX_AF_UNIX) { if (addrlen <= sizeof(addr->sa_family)) return -L_EINVAL; if (addr->sa_family != LINUX_AF_UNIX) return -L_EAFNOSUPPORT; const struct sockaddr_un *addr_un = (const struct sockaddr_un *)addr; log_unix_socket_addr(addr_un, addrlen); if (addrlen == 0) { log_warning("sockaddr is empty."); return -L_EINVAL; } if (addr_un->sun_path[0] == 0) { log_error("Abstract sockaddr not supported."); return -L_EINVAL; } /* Get socket port */ struct file *winfile; int r = vfs_openat(AT_FDCWD, addr_un->sun_path, O_RDONLY, 0, 0, &winfile); if (r < 0) return r; char buf[10]; r = winfs_read_special_file(winfile, WINFS_UNIX_HEADER, WINFS_UNIX_HEADER_LEN, buf, sizeof(buf)); vfs_release(winfile); if (r < 0) { log_warning("Open socket file failed."); return r; } if (r == 0) /* The file is not a socket file */ { log_warning("Not a socket file."); return -L_ECONNREFUSED; } buf[r] = 0; int port; if (!katoi(buf, &port)) { log_warning("Invalid socket file content."); return -L_ECONNREFUSED; } log_info("port: %d", port); /* Generate inet sockaddr for winsock */ struct sockaddr_in *addr_inet = (struct sockaddr_in *)&addr_storage; addr_storage_len = sizeof(struct sockaddr_in); memset(addr_inet, 0, sizeof(struct sockaddr_in)); addr_inet->sin_family = AF_INET; addr_inet->sin_addr.S_un.S_addr = htonl(INADDR_LOOPBACK); addr_inet->sin_port = htons(port); } else if ((addr_storage_len = translate_socket_addr_to_winsock((const struct sockaddr_storage *)addr, &addr_storage, addrlen)) == SOCKET_ERROR) return -L_EINVAL; WaitForSingleObject(socket->mutex, INFINITE); int r = 0; if (connect(socket->socket, (struct sockaddr *)&addr_storage, addr_storage_len) == SOCKET_ERROR) { int err = WSAGetLastError(); if (err != WSAEWOULDBLOCK) { log_warning("connect() failed, error code: %d", err); r = translate_socket_error(err); } else if ((f->flags & O_NONBLOCK) > 0) { log_info("connect() returned EINPROGRESS."); r = -L_EINPROGRESS; } else { socket_wait_event(socket, FD_CONNECT, 0); r = translate_socket_error(WSAGetLastError()); } } ReleaseMutex(socket->mutex); return r; }