/* test of proc_user/server_identify: recv part Copyright (C) 2015 Free Software Foundation, Inc. Written by Svante Signell . This file is part of the GNU Hurd. The GNU Hurd is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. The GNU Hurd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the GNU Hurd. If not, see . */ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #define SOCK_PATH "ident_socket" int main (void) { pid_t p_pid= getpid (), pid = -1; mach_port_t rendezvous = MACH_PORT_NULL; mach_port_t newport = MACH_PORT_NULL; error_t err; #ifdef USE_SOCK_DGRAM int sfd; #else int sfd, lfd; #endif struct sockaddr_un addr = { 0 }; mach_port_t addrport; char rbuf[30]; char *rbufp = rbuf; vm_size_t nr = sizeof(rbuf); mach_msg_type_number_t nread; mach_port_t *ports; mach_msg_type_number_t nports = 0; char *cdata = NULL; mach_msg_type_number_t clen = 0; int flags; /* Create socket bound to well-known address */ err = remove (SOCK_PATH); if (err == -1 && errno != ENOENT) { perror ("remove"); exit(EXIT_FAILURE); } printf ("ident_socket_recv.c: p_pid = %d\n", p_pid); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, SOCK_PATH, sizeof(addr.sun_path) - 1); #ifdef USE_SOCK_DGRAM sfd = socket (AF_UNIX, SOCK_DGRAM, 0); #else sfd = socket (AF_UNIX, SOCK_STREAM, 0); #endif if (sfd == -1) { perror ("socket"); exit(EXIT_FAILURE); } printf("ident_socket_recv.c: Socket file descriptor = %d\n", sfd); err = bind (sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)); if (err == -1) { close(sfd); perror ("bind"); exit(EXIT_FAILURE); } #ifdef USE_SOCK_DGRAM printf ("ident_socket_recv.c: Receiving via datagram socket\n"); /* Receive data */ err = HURD_DPORT_USE (sfd, __socket_recv (port, &addrport, 0, &rbufp, &nread, &ports, &nports, &cdata, &clen, &flags, nr)); #else /* SOCK_STREAM */ printf ("ident_socket_recv.c: Receiving via stream socket\n"); err = listen (sfd, 1); if (err == -1) { perror ("listen"); exit(EXIT_FAILURE); } lfd = accept (sfd, NULL, NULL); if (lfd == -1) { perror ("accept"); exit(EXIT_FAILURE); } /* Receive data */ err = HURD_DPORT_USE (lfd, __socket_recv (port, &addrport, 0, &rbufp, &nread, &ports, &nports, &cdata, &clen, &flags, nr)); #endif if (err) { printf("ident_socket_recv.c: __socket_recv() err = %d \n", err); return err; } if (nread == -1) { perror ("__socket_recv"); exit(EXIT_FAILURE); } printf("ident_socket_recv.c: __socket_recv() returned %ld bytes\n", (long) nread); rendezvous = ports[0]; if (nread > 0) printf("ident_socket_recv.c: Received data = %d\n", atoi(rbuf)); if (MACH_PORT_VALID (rendezvous)) printf ("ident_socket_recv.c: rendezvous port = %d\n", (int)rendezvous); else printf ("ident_socket_recv.c: rendezvous not valid\n"); err = mach_port_mod_refs (mach_task_self (), rendezvous, MACH_PORT_RIGHT_SEND, +1); if (err) { printf ("ident_socket_recv.c: mach_port_mod_refs() err = %d\n", err); goto out; } #if 0 do err = __USEPORT (PROC, proc_server_identify (port, rendezvous, MACH_MSG_TYPE_COPY_SEND, newport, MACH_MSG_TYPE_COPY_SEND, &pid)); while (err == EINTR); #endif if (err) goto out; /* Print *id */ printf("ident_socket_recv.c: proc_server_identify(): Received pid = %d\n", (int)pid); out: mach_port_deallocate (mach_task_self (), rendezvous); mach_port_deallocate (mach_task_self (), newport); if (err) printf ("ident_socket_recv.c: err = %d\n", err); return err; }