From: Andre Nathan Date: 2008-08-29T09:20:44+09:00 Subject: Re: Calling sendmsg() from Ruby On Tue, 2008-08-26 at 04:30 +0900, Andre Nathan wrote: > I'm trying to call the sendmsg() system call from ruby in a way that > allocates space for the kernel to set the user credentials in the > appropriate structures (this is in FreeBSD i386). For the record, this is how I got it to work. I was wrong in the way I was packing cmsg_data, and I also had forgotten passing an argument to the syscall (duh). def sendmsg(fd, buf) iov = [buf, buf.length].pack("pI_") cmsg_len = 96 # CMSG_LEN(sizeof(struct cmsgcred)) cmsg_space = 96 # CMSG_SPACE(sizeof(struct cmsgcred)) cmsg_level = 0xffff # SOL_SOCKET cmsg_type = 0x03 # SCM_CREDS cmsg_data_len = 84 cmsghdr = ([ cmsg_len, cmsg_level, cmsg_type ] + [0] * cmsg_data_len).pack("I_i_i_C#{cmsg_data_len}") msg_control_ptr = pointer(cmsghdr) msg_controllen = cmsg_space msghdr = [ 0, # msg_name 0, # msg_namelen pointer(iov), # msg_iov 1, # msg_iovlen pointer(cmsghdr), # msg_control cmsg_space, # msg_controllen 0 # msg_flags ].pack("L_i_L_i_L_i_i_") syscall(28, fd.fileno, pointer(msghdr), 0) end