chiark / gitweb /
fishdescriptor: More work, before trying to compile it
[chiark-utils.git] / fishdescriptor / donate.c
index 60ca2666379bfaed7e52d1c38411349905057638..49e244d9c53b403c522f619ce738fec89e31c28b 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;
 }