chiark / gitweb /
fishdescriptor: More work, before trying to compile it
authorIan Jackson <ian.jackson@eu.citrix.com>
Thu, 5 Oct 2017 16:10:00 +0000 (17:10 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Thu, 5 Oct 2017 16:10:00 +0000 (17:10 +0100)
Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
fishdescriptor/Makefile [new file with mode: 0644]
fishdescriptor/donate.c

diff --git a/fishdescriptor/Makefile b/fishdescriptor/Makefile
new file mode 100644 (file)
index 0000000..17bb3e7
--- /dev/null
@@ -0,0 +1,4 @@
+fishdescriptor-donate.so:      donate.o
+       $(CC) -shared -soname $(LIBCANON) -o $@ $< $(LIBS)
+
+donate.o:                      donate.c
index 60ca266..49e244d 100644 (file)
@@ -16,7 +16,8 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* return conventions: functions here return errno values */
+/* return conventions: functions here return 0 on success,
+ * or -1 setting errno */
 
 static int fishdescriptor_sendmsg_fds(int carrier,
                                       int nfds, const int fds[]) {
@@ -46,6 +47,35 @@ static int fishdescriptor_sendmsg_fds(int carrier,
   msg.msg_controllen = cmsg->cmsg_len;
     
   r = sendmsg(carrier, &msg, 0);
-  if (r < 0)
-    return 0;
+  if (r < 0) return -1;
+
+  return 0;
+}
+
+static int fishdescriptor_donate(const char *path, const int *fds) {
+  int r;
+  int carrier=-1;
+
+  carrier = socket(AF_UNIX, SOCK_STREAM, 0);
+  if (carrier < 0) goto out;
+
+  struct sockaddr_un suna;
+  memset(suna,0,&suna);
+  suna.sun_family = AF_UNIX;
+  if (strlen(path) >= sizeof(suna.sun_path)) { outno = E2BIG; goto out; }
+  strcpy(suna.sun_path, path);
+
+  int r = connect(carrier, &suna, sizeof(suna));
+  if (r) goto out;
+
+  int nfds;
+  for (nfds=0; fds[nfds] > 0; nfds++);
+
+  r = fishdescriptor_sendmsg_fds(carrier, nfds, fds);
+  if (r) goto out;
+
+  r = 0;
+ out:
+  if (carrier >= 0) close(carrier);
+  return r;
 }