├── src ├── .gitignore ├── ipc-config │ ├── .gitignore │ ├── Makefile │ └── ipc-config.sh ├── BUGS ├── ipcc │ ├── BUGS │ └── ipcc.rb ├── ipc_private.h ├── fdpass.h ├── log.c ├── configure ├── log.h ├── Makefile.OLD ├── fdpass.c ├── ipcd.c └── ipc.c ├── doc ├── handbook │ ├── .gitignore │ ├── Makefile │ ├── COPYING │ ├── introduction.xml │ ├── handbook.xml │ └── examples.xml ├── ipcd.8 ├── ipc_bind.3 ├── ipc_accept.3 ├── ipc.7 └── ipc_connect.3 ├── testing ├── .gitignore ├── ipcc-1 │ ├── com.example.myservice.ipc │ ├── test-harness.sh │ ├── client.c │ ├── Makefile │ └── server.c ├── ipcc-2 │ ├── com.example.myservice.ipc │ ├── Makefile │ ├── test-harness.sh │ ├── client.c │ └── server.c ├── nvlist-experiment │ ├── Makefile │ ├── client.c │ └── server.c ├── Makefile.OLD ├── configure ├── test-harness.sh ├── pingpong-client.c └── pingpong-server.c ├── vendor └── libkqueue-2.0.3.tar.gz ├── .travis.yml ├── .gitignore ├── Makefile.OLD ├── include ├── Makefile └── ipc.h ├── install-dir.mk ├── LICENSE ├── configure ├── README.md └── config.sub /src/.gitignore: -------------------------------------------------------------------------------- 1 | ipcd 2 | -------------------------------------------------------------------------------- /doc/handbook/.gitignore: -------------------------------------------------------------------------------- 1 | tmp/ 2 | -------------------------------------------------------------------------------- /src/ipc-config/.gitignore: -------------------------------------------------------------------------------- 1 | ipc-config$ 2 | -------------------------------------------------------------------------------- /testing/.gitignore: -------------------------------------------------------------------------------- 1 | pingpong-server 2 | pingpong-client 3 | -------------------------------------------------------------------------------- /vendor/libkqueue-2.0.3.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mheily/libipc/HEAD/vendor/libkqueue-2.0.3.tar.gz -------------------------------------------------------------------------------- /testing/ipcc-1/com.example.myservice.ipc: -------------------------------------------------------------------------------- 1 | --- 2 | service: com.example.myservice 3 | domain: IPC_DOMAIN_USER 4 | methods: 5 | echo: 6 | id: 1 7 | prototype: int echo(int *response, int request) 8 | -------------------------------------------------------------------------------- /testing/ipcc-2/com.example.myservice.ipc: -------------------------------------------------------------------------------- 1 | --- 2 | service: com.example.myservice 3 | domain: IPC_DOMAIN_USER 4 | methods: 5 | pingpong: 6 | id: 1 7 | prototype: int pingpong(char **reply, const char *request) 8 | -------------------------------------------------------------------------------- /testing/nvlist-experiment/Makefile: -------------------------------------------------------------------------------- 1 | all: client server 2 | 3 | client: client.c 4 | cc -g -o client client.c -lnv 5 | 6 | server: server.c 7 | cc -g -o server server.c -lnv 8 | 9 | clean: 10 | rm -f client server -------------------------------------------------------------------------------- /src/BUGS: -------------------------------------------------------------------------------- 1 | Known bugs 2 | ========== 3 | 4 | Improvements 5 | ============ 6 | * log.c and log.h are copy-pasta from stated. The same logging mechanism is used in relaunchd. 7 | It would be good to create a first-class project for logging, or find a better replacement. 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | sudo: false 4 | 5 | # DEADWOOD: prefer to use the vendored libkqueue 6 | #addons: 7 | # apt: 8 | # packages: 9 | # - libkqueue0 10 | # - libkqueue-dev 11 | 12 | cache: 13 | directories: 14 | - vendor/libkqueue-2.0.3 15 | 16 | script: 17 | - ./configure 18 | - make 19 | - make check 20 | -------------------------------------------------------------------------------- /doc/handbook/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # See the COPYING file for license information 3 | # 4 | 5 | clean: 6 | rm -rf tmp 7 | 8 | preview: 9 | mkdir -p tmp 10 | xmlto -o tmp html handbook.xml 11 | firefox ./tmp/index.html 12 | 13 | publish: 14 | git checkout gh-pages 15 | git merge master 16 | xmlto html *.xml 17 | git add *.html 18 | git commit -m'publishing it' 19 | git push 20 | git checkout master 21 | 22 | .PHONY: clean preview publish -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | 34 | # Eclipse 35 | .cproject 36 | .project 37 | 38 | # ipcc generated files 39 | *.skeleton.c 40 | *.skeleton.h 41 | *.stub.c 42 | *.stub.h 43 | 44 | # Other generated files 45 | config.h 46 | config.log 47 | config.mk 48 | vars.sh 49 | -------------------------------------------------------------------------------- /src/ipcc/BUGS: -------------------------------------------------------------------------------- 1 | Things that don't work: 2 | 3 | * Passing things that sizeof() doesn't return the correct size for. 4 | - Is there any such thing? sizeof() will work on variable-length arrays. 5 | 6 | * Passing or returning variable-length strings. 7 | - Solution: Have a variable-length part of the response. The server replaces pointers 8 | with offsets into this variable-length area. The offsets will be converted back into 9 | pointers by the client. 10 | 11 | * Passing structures that contain pointers. 12 | - Solution: Use clang to examine the structure to find pointers. Will be very hard. 13 | 14 | * A function that returns no values. 15 | - Solution: declare a surrogate for 'void' such as an 'int' that is ignored. 16 | -------------------------------------------------------------------------------- /doc/handbook/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Mark Heily 2 | 3 | Permission to use, copy, modify, and/or distribute this document for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THIS DOCUMENT IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS DOCUMENT INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OF THIS DOCUMENT. 6 | -------------------------------------------------------------------------------- /testing/Makefile.OLD: -------------------------------------------------------------------------------- 1 | CFLAGS+=-Wl,-rpath,../src 2 | CFLAGS+=-I../include -I../src -DDEBUG -g -O0 3 | LDFLAGS+=-L../src 4 | LDADD+=-lipc 5 | TESTS= pingpong-server pingpong-client 6 | 7 | all: 8 | 9 | clean: 10 | rm -f $(TESTS) 11 | $(MAKE) -C ipcc-1 clean 12 | $(MAKE) -C ipcc-2 clean 13 | 14 | ../src/libipc.so: 15 | cd ../src && $(MAKE) libipc.so 16 | 17 | pingpong-server: 18 | $(CC) $(CFLAGS) $(LDFLAGS) -o pingpong-server pingpong-server.c ../src/log.c $(LDADD) 19 | 20 | pingpong-client: 21 | $(CC) $(CFLAGS) $(LDFLAGS) -o pingpong-client pingpong-client.c ../src/log.c $(LDADD) 22 | 23 | check: 24 | # FIXME: this is broken 25 | #./test-harness.sh 26 | cd ipcc-1 && make check 27 | cd ipcc-2 && make check 28 | 29 | .PHONY: ipcd check 30 | -------------------------------------------------------------------------------- /testing/ipcc-2/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | include ../ipcc-1/Makefile 18 | -------------------------------------------------------------------------------- /doc/handbook/introduction.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | What is libipc? 8 | 9 |
10 | Overview 11 | 12 | libipc is a mechanism that allows a C program to call a function that 13 | is executed by a different program running on the same machine. This is 14 | one of the major uses for inter-process communication. 15 | 16 |
17 | 18 |
19 | Use cases 20 | 21 | libipc is useful for the following types of programs: 22 | 23 | controlling daemons 24 | running code in a sandbox that needs to communicate with other processes 25 | 26 | 27 |
28 |
-------------------------------------------------------------------------------- /testing/configure: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2016 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | . ../config.sub 19 | 20 | SUBDIRS="ipcc-1 ipcc-2" 21 | 22 | write_makefile 23 | -------------------------------------------------------------------------------- /src/ipc_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ipc_private.h 3 | * 4 | * Created on: Dec 26, 2015 5 | * Author: mark 6 | */ 7 | 8 | #ifndef SRC_IPC_PRIVATE_H_ 9 | #define SRC_IPC_PRIVATE_H_ 10 | 11 | /* All symbols are hidden by default. Use this macro to declare symbols 12 | * that are part of the API. 13 | */ 14 | #define VISIBLE __attribute__ ((visibility ("default"))) 15 | 16 | /*# An in-progress operation */ 17 | typedef struct { 18 | enum { 19 | IPC_OP_BIND = 0, 20 | IPC_OP_CONNECT = 1, 21 | } opcode; 22 | char name[IPC_SERVICE_NAME_MAX]; 23 | size_t namelen; 24 | uid_t uid; 25 | gid_t gid; 26 | } ipc_operation_t; 27 | 28 | /* KLUDGE: not sure why this isn't visible */ 29 | int getpeereid(int, uid_t *, gid_t *); 30 | 31 | static inline char * 32 | opcode_to_str(int opcode) 33 | { 34 | switch (opcode) { 35 | case IPC_OP_BIND: 36 | return "bind"; 37 | case IPC_OP_CONNECT: 38 | return "connect"; 39 | default: 40 | return "invalid-opcode"; 41 | } 42 | } 43 | 44 | #endif /* SRC_IPC_PRIVATE_H_ */ 45 | -------------------------------------------------------------------------------- /Makefile.OLD: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | include install-dir.mk 18 | 19 | default: all 20 | 21 | check: 22 | cd testing && $(MAKE) check 23 | 24 | install uninstall clean all: 25 | $(MAKE) -C src $@ 26 | $(MAKE) -C include $@ 27 | $(MAKE) -C testing $@ 28 | -------------------------------------------------------------------------------- /src/fdpass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #ifndef FDPASS_H_ 18 | #define FDPASS_H_ 19 | 20 | int fdpass_send(int socket, int fd, void *base, size_t len); 21 | int fdpass_recv(int socket, void *base, socklen_t *len); 22 | 23 | #endif /* FDPASS_H_ */ 24 | -------------------------------------------------------------------------------- /include/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | include ../install-dir.mk 18 | 19 | default: all 20 | 21 | install: 22 | $(INSTALL) -m 644 ipc.h $$DESTDIR$(INCLUDEDIR) 23 | 24 | uninstall: 25 | rm -f $$DESTDIR$(INCLUDEDIR)/ipc.h 26 | 27 | clean check all: 28 | @true 29 | 30 | .PHONY: install uninstall 31 | -------------------------------------------------------------------------------- /src/ipc-config/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2016 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | include ../../install-dir.mk 18 | 19 | default: all 20 | 21 | all: ipc-config 22 | 23 | ipc-config: ipc-config.sh 24 | $(INSTALL) -m 555 ipc-config.sh ipc-config 25 | 26 | check: 27 | @true 28 | 29 | clean: 30 | rm -f ipc-config 31 | 32 | install uninstall: 33 | @false 34 | -------------------------------------------------------------------------------- /install-dir.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2016 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | # Installation directories 18 | # 19 | PREFIX ?= /usr/local 20 | LIBDIR = $(PREFIX)/lib 21 | INCLUDEDIR = $(PREFIX)/include 22 | BINDIR = $(PREFIX)/bin 23 | SBINDIR = $(PREFIX)/sbin 24 | LIBEXECDIR = $(PREFIX)/libexec 25 | MANDIR = $(PREFIX)/man 26 | SYSCONFDIR = $(PREFIX)/etc 27 | DATADIR = $(PREFIX)/share 28 | LOCALSTATEDIR = /var 29 | RUNSTATEDIR = $(LOCALSTATEDIR)/run 30 | -------------------------------------------------------------------------------- /testing/ipcc-1/test-harness.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2015 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | make -C ../.. clean all || exit 19 | make clean all || exit 20 | 21 | rm -f ~/.ipc/services/com.example.myservice 22 | 23 | ./test-server & 24 | server_pid=$! 25 | echo "launched server on pid $server_pid" 26 | 27 | # Ensure the server has time to bind to the name 28 | sleep 1 29 | 30 | ./test-client 31 | kill $server_pid 32 | 33 | exit 0 34 | -------------------------------------------------------------------------------- /testing/ipcc-2/test-harness.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2015 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | make -C ../.. clean all || exit 19 | make clean all || exit 20 | 21 | rm -f ~/.ipc/services/com.example.myservice 22 | 23 | ./test-server & 24 | server_pid=$! 25 | echo "launched server on pid $server_pid" 26 | 27 | # Ensure the server has time to bind to the name 28 | sleep 1 29 | 30 | ./test-client 31 | kill $server_pid 32 | 33 | exit 0 34 | -------------------------------------------------------------------------------- /testing/test-harness.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2015 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | make clean all || exit 19 | 20 | #cd ../src 21 | #make clean ipcd || exit 22 | #./ipcd & 23 | #ipcd_pid=$? 24 | #sleep 1 # let it have time to bind() and so forth 25 | #echo "launched ipcd on pid $ipcd_pid" 26 | 27 | cd ../testing 28 | ./pingpong-server & 29 | server_pid=$? 30 | echo "launched pingpong-server on pid $server_pid" 31 | 32 | # Ensure the server has time to bind to the name 33 | sleep 3 34 | 35 | ./pingpong-client 36 | kill $server_pid 37 | #kill $ipcd_pid 38 | -------------------------------------------------------------------------------- /src/log.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | #include "log.h" 21 | 22 | char *log_ident; 23 | FILE *logfile; 24 | 25 | int log_open(const char *ident, const char *path) 26 | { 27 | log_ident = strdup(ident); 28 | if (!log_ident) return -1; 29 | if (!path) return -1; 30 | logfile = fopen(path, "a"); 31 | if (!logfile) return -1; 32 | return (0); 33 | log_info("log started"); 34 | } 35 | 36 | int log_close(void) 37 | { 38 | if (!logfile) return -1; 39 | return fclose(logfile); 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Mark Heily 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | -------------------------------------------------------------------------------- /doc/handbook/handbook.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | libipc Development Guide 8 | Jan 9, 2016 9 | MarkHeily 10 | 11 | 2016 12 | Mark Heily 13 | 14 | 15 | 16 | 17 | Permission to use, copy, modify, and/or distribute this document for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 18 | 19 | 20 | THIS DOCUMENT IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS DOCUMENT INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OF THIS DOCUMENT. 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /configure: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2016 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | . ./config.sub 19 | 20 | package 'libipc' '0.0.1' 21 | 22 | check_header 'sys/event.h' 23 | if [ $check_header_sys_event_h -eq 0 ] ; then 24 | echo "building a local copy of libkqueue.. " 25 | tar -C vendor -xf ./vendor/libkqueue-2.0.3.tar.gz 26 | make_define 'kqueue_dir' "`pwd`/vendor/libkqueue-2.0.3" 27 | make_define 'kqueue_CFLAGS' "-I$kqueue_dir/include" 28 | make_define 'kqueue_LDADD' '$kqueue_dir/.libs/libkqueue.a -lpthread -lrt' 29 | make_define 'kqueue_DEPENDS' "$kqueue_dir/.libs/libkqueue.so" 30 | target "$kqueue_dir/.libs/libkqueue.a: 31 | cd $kqueue_dir && ./configure && make 32 | 33 | " 34 | else 35 | make_define 'kqueue_CFLAGS' '' 36 | make_define 'kqueue_LDADD' '' 37 | make_define 'kqueue_DEPENDS' '' 38 | fi 39 | 40 | SUBDIRS=src 41 | 42 | write_makefile -------------------------------------------------------------------------------- /testing/pingpong-client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../include/ipc.h" 31 | #include "log.h" 32 | 33 | int main(int argc, char *argv[]) { 34 | int server; 35 | char buf[5]; 36 | ssize_t bytes; 37 | int result; 38 | 39 | log_open("pingpong-client", "/dev/stderr"); 40 | 41 | log_debug("client started"); 42 | 43 | server = ipc_connect(IPC_DOMAIN_USER, "test.ping", 1); 44 | if (server < 0) 45 | errx(1, "connect"); 46 | 47 | bytes = read(server, &buf, 5); 48 | if (result < 5) 49 | err(1, "read: %zu bytes", bytes); 50 | 51 | printf("client got: %s\n", buf); 52 | 53 | result = ipc_close(server); 54 | if (result < 0) 55 | err(1, "close: %s", ipc_strerror(result)); 56 | 57 | exit(EXIT_SUCCESS); 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libipc 2 | 3 | libipc is a mechanism that allows a C program to call a function that is 4 | executed in a different program. It is implemented as a thin layer on top 5 | of Unix-domain sockets. 6 | 7 | It is under heavy development and the documentation has not been written yet. 8 | 9 | It currently runs on FreeBSD and Linux, but should be portable to 10 | other POSIX-like systems. 11 | 12 | What currently works: 13 | * using the ipcc IDL compiler to generate code 14 | * declaring functions that take integers or strings 15 | * embedding an IPC server into an existing daemon 16 | * calling remote functions as a client 17 | 18 | What is planned for the future: 19 | * support for passing file descriptors between processes 20 | * declaring structures and passing them as function arguments 21 | * merging [the StateD library](https://github.com/mheily/stated) into libipc, 22 | and using it to provide support for variables 23 | * thread safety 24 | * a convenience function to call from main() that provides a complete IPC server-in-a-box. 25 | This would be for daemons that are purely for IPC, and don't have their own run loop. 26 | 27 | What would be desired, but is not on the roadmap yet: 28 | * asynchronous function calls 29 | * kernel support for performance optimizations 30 | 31 | # Building from source code 32 | 33 | ## Building on FreeBSD 34 | 35 | Run "make" to build everything. 36 | 37 | ##Building on Linux 38 | 39 | 1. Install libkqueue 40 | 2. Run: 41 | ``` 42 | make CFLAGS="-I/usr/include/kqueue -D_BSD_SOURCE" LDADD="-lkqueue -ldl -lpthread" 43 | ``` 44 | 45 | # Usage 46 | 47 | Refer to the [Developer's Guide](http://mheily.github.io/libipc/) for 48 | information about using this library. 49 | 50 | # Contact information 51 | 52 | There is a [libipc-devel mailing list](https://groups.google.com/d/forum/libipc-devel) dedicated 53 | to discussion of this library. 54 | 55 | -------------------------------------------------------------------------------- /testing/ipcc-1/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../../include/ipc.h" 31 | #include "../../src/log.h" 32 | #include 33 | 34 | void call_echo() 35 | { 36 | int rv; 37 | int ret1 = -1; 38 | int arg1 = 123; 39 | 40 | rv = echo(&ret1, arg1); 41 | if (rv != 0) 42 | errx(1, "FAIL: %s", ipc_strerror(rv)); 43 | if (ret1 != 123) 44 | errx(1, "FAIL: unexpected return value"); 45 | } 46 | 47 | int main(int argc, char *argv[]) 48 | { 49 | int server; 50 | char buf[5]; 51 | ssize_t bytes; 52 | int result; 53 | 54 | setenv("IPC_LIBDIR", "./ipc", 1); 55 | log_open("client", "/dev/stderr"); 56 | ipc_openlog("client", "/dev/stderr"); 57 | 58 | call_echo(); 59 | 60 | log_notice("success; exiting normally"); 61 | exit(EXIT_SUCCESS); 62 | } 63 | -------------------------------------------------------------------------------- /src/configure: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2016 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | . ../config.sub 19 | . ../vars.sh 20 | 21 | check_header 'sys/event.h' 22 | if [ $check_header_sys_event_h -eq 0 ] ; then 23 | echo "building a local copy of libkqueue.. " 24 | tar -C ../vendor -xf ../vendor/libkqueue-2.0.3.tar.gz 25 | make_define 'kqueue_dir' "`pwd`/../vendor/libkqueue-2.0.3" 26 | make_define 'kqueue_CFLAGS' "-I$kqueue_dir/include" 27 | make_define 'kqueue_LDADD' '$kqueue_dir/.libs/libkqueue.so -lpthread -lrt' 28 | make_define 'kqueue_DEPENDS' "$kqueue_dir/.libs/libkqueue.so" 29 | target "$kqueue_dir/.libs/libkqueue.so: 30 | cd $kqueue_dir && ./configure && make 31 | 32 | " 33 | else 34 | make_define 'kqueue_CFLAGS' '' 35 | make_define 'kqueue_LDADD' '' 36 | make_define 'kqueue_DEPENDS' '' 37 | fi 38 | 39 | LIBRARIES=libipc 40 | 41 | libipc_SOURCES="ipc.c log.c fdpass.c" 42 | libipc_CFLAGS="-Wall -Werror -std=c99 $kqueue_CFLAGS" 43 | libipc_LDFLAGS="$kqueue_LDFLAGS" 44 | libipc_LDADD="$kqueue_LDADD" 45 | libipc_SONAME="libipc.so.1" 46 | libipc_REALNAME="libipc.so.1.0.1" 47 | libipc_DEPENDS="$kqueue_DEPENDS" 48 | 49 | write_makefile 50 | -------------------------------------------------------------------------------- /testing/ipcc-2/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../../include/ipc.h" 31 | #include "../../src/log.h" 32 | #include 33 | 34 | void call_pingpong() 35 | { 36 | int rv; 37 | char *ret1; 38 | char *arg1 = "ping"; 39 | 40 | rv = pingpong(&ret1, arg1); 41 | if (rv != 0) 42 | errx(1, "FAIL: %s", ipc_strerror(rv)); 43 | if (!ret1 || strcmp(ret1, "pong") != 0) 44 | errx(1, "FAIL: unexpected return value"); 45 | free(ret1); 46 | } 47 | 48 | int main(int argc, char *argv[]) 49 | { 50 | int server; 51 | char buf[5]; 52 | ssize_t bytes; 53 | int result; 54 | 55 | setenv("IPC_LIBDIR", "./ipc", 1); 56 | log_open("client", "/dev/stderr"); 57 | ipc_openlog("client", "/dev/stderr"); 58 | 59 | call_pingpong(); 60 | 61 | log_notice("success; exiting normally"); 62 | exit(EXIT_SUCCESS); 63 | } 64 | -------------------------------------------------------------------------------- /testing/ipcc-1/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | test_LDFLAGS+=-Wl,-rpath,../../src -L../../src 18 | test_LDFLAGS+=-Wl,-rpath,./ipc -L./ipc 19 | test_CFLAGS+=-std=c99 -I../../include -I. -I./ipc -DDEBUG -g -O0 20 | test_LDADD+=../../src/log.c 21 | 22 | test_CFLAGS+=$(CFLAGS) 23 | test_LDFLAGS+=$(LDFLAGS) 24 | test_LDADD+=$(LDADD) 25 | 26 | IPCC= ../../src/ipcc/ipcc.rb 27 | unused_IPC_CONFIG= IPC_CONFIG_LIBDIR=. \ 28 | IPC_CONFIG_INCLUDEDIR=. \ 29 | ../../src/ipc-config/ipc-config 30 | 31 | all: ipc/libipc_com_example_myservice.so test-server test-client 32 | 33 | ipc/libipc_com_example_myservice.so: 34 | mkdir -p ipc 35 | $(IPCC) --debug \ 36 | --cflags="-I../../include" \ 37 | --ldflags="-Wl,-rpath,../../src -L../../src" \ 38 | --c-out=./ipc com.example.myservice.ipc 39 | 40 | test-client: 41 | $(CC) $(test_CFLAGS) $(test_LDFLAGS) -o test-client client.c $(test_LDADD) -lipc_debug 42 | 43 | test-server: 44 | $(CC) $(test_CFLAGS) -rdynamic $(test_LDFLAGS) -o test-server server.c $(test_LDADD) -lipc_debug 45 | 46 | check: 47 | ./test-harness.sh 48 | 49 | clean: 50 | rm -f *.ipc.c *.ipc.h 51 | rm -f test-server test-client 52 | rm -rf ./ipc 53 | 54 | .PHONY: clean 55 | -------------------------------------------------------------------------------- /doc/ipcd.8: -------------------------------------------------------------------------------- 1 | .\" $​Id$ 2 | .\" Copyright (c) 2015, Mark Heily 3 | .\" All rights reserved. 4 | .\" 5 | .\" Redistribution and use in source and binary forms, with or without 6 | .\" modification, are permitted provided that the following conditions are met: 7 | .\" 8 | .\" * Redistributions of source code must retain the above copyright notice, this 9 | .\" list of conditions and the following disclaimer. 10 | .\" 11 | .\" * Redistributions in binary form must reproduce the above copyright notice, 12 | .\" this list of conditions and the following disclaimer in the documentation 13 | .\" and/or other materials provided with the distribution. 14 | .\" 15 | .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | .\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | .\" 26 | .Dd December 26, 2015 27 | .Dt ipcd 8 28 | .Os 29 | .Sh NAME 30 | .Nm ipcd 31 | .Nd an IPC namespace management daemon 32 | .Sh SYNOPSIS 33 | .Nm 34 | .Sh DESCRIPTION 35 | Processes running under a non-privileged user ID may communicate with 36 | .Nm 37 | to request the ability to bind to the IPC namespace. 38 | .Sh EXIT STATUS 39 | .Ex -std 40 | .Sh ENVIRONMENT 41 | This daemon does not rely on any environment variables. 42 | .Sh AUTHORS 43 | The 44 | .Nm 45 | daemon was written by 46 | .An Mark Heily , 47 | .Mt mark@heily.com . -------------------------------------------------------------------------------- /doc/ipc_bind.3: -------------------------------------------------------------------------------- 1 | .\" $​Id$ 2 | .\" Copyright (c) 2015, Mark Heily 3 | .\" All rights reserved. 4 | .\" 5 | .\" Redistribution and use in source and binary forms, with or without 6 | .\" modification, are permitted provided that the following conditions are met: 7 | .\" 8 | .\" * Redistributions of source code must retain the above copyright notice, this 9 | .\" list of conditions and the following disclaimer. 10 | .\" 11 | .\" * Redistributions in binary form must reproduce the above copyright notice, 12 | .\" this list of conditions and the following disclaimer in the documentation 13 | .\" and/or other materials provided with the distribution. 14 | .\" 15 | .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | .\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | .\" 26 | .Dd December 26, 2015 27 | .Dt ipc_bind 3 28 | .Os 29 | .Sh NAME 30 | .Nm ipc_bind 31 | .Nd register a service within the IPC namespace 32 | .Sh LIBRARY 33 | libipc, -lipc 34 | .Sh SYNOPSIS 35 | .In ipc.h 36 | .Ft int 37 | .Fo ipc_bind 38 | .Fa "int domain" 39 | .Fa "const char *service" 40 | .Fa "int version" 41 | .Fc 42 | .Sh DESCRIPTION 43 | The 44 | .Fn ipc_bind 45 | function registers a service name in the IPC namespace. 46 | .Sh RETURN VALUES 47 | .Rv -std 48 | .Sh SEE ALSO 49 | .Xr ipc_connect 3 , 50 | .Xr ipc 7 , 51 | .Xr ipcd 8 52 | .Sh AUTHORS 53 | .An Mark Heily 54 | .Mt . -------------------------------------------------------------------------------- /src/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #ifndef LOG_H_ 18 | #define LOG_H_ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | /* Logging */ 25 | 26 | extern FILE *logfile; 27 | extern char * log_ident; 28 | 29 | int log_open(const char *ident, const char *path); 30 | int log_close(void); 31 | 32 | /* Kludgy because this doesn't use syslog(3) yet */ 33 | #define _log_all(level, format,...) do { \ 34 | (void)level; \ 35 | if (logfile) { \ 36 | fprintf(logfile, \ 37 | "[%s] %s(%s:%d): "format"\n", \ 38 | log_ident, __func__, __FILE__, __LINE__, ## __VA_ARGS__); \ 39 | fflush(logfile); \ 40 | } \ 41 | } while (0) 42 | 43 | #define log_error(format,...) _log_all(LOG_ERR, "**ERROR** "format, ## __VA_ARGS__) 44 | #define log_warning(format,...) _log_all(LOG_WARNING, "WARNING: "format, ## __VA_ARGS__) 45 | #define log_notice(format,...) _log_all(LOG_NOTICE, format, ## __VA_ARGS__) 46 | #define log_info(format,...) _log_all(LOG_INFO, format, ## __VA_ARGS__) 47 | #ifdef DEBUG 48 | #define log_debug(format,...) _log_all(LOG_DEBUG, format, ## __VA_ARGS__) 49 | #else 50 | #define log_debug(format,...) do { (void)format; } while (0) 51 | #endif 52 | #define log_errno(format,...) _log_all(LOG_ERR, format": %s", ## __VA_ARGS__, strerror(errno)) 53 | 54 | #endif /* LOG_H_ */ 55 | -------------------------------------------------------------------------------- /testing/nvlist-experiment/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int setup_client_socket() { 12 | char statedir[PATH_MAX]; 13 | struct sockaddr_un sock; 14 | int len, fd; 15 | 16 | sock.sun_family = AF_LOCAL; 17 | len = snprintf(sock.sun_path, sizeof(sock.sun_path), "%s/foo.sock", "/usr/home/mark/tmp"); 18 | 19 | if (len >= sizeof(sock.sun_path)) { 20 | //client->last_error = -IPC_ERROR_NAME_TOO_LONG; 21 | return -1; 22 | } 23 | if (len < 0) { 24 | //client->last_error = IPC_CAPTURE_ERRNO; 25 | return -1; 26 | } 27 | 28 | fd = socket(AF_LOCAL, SOCK_STREAM, 0); 29 | if (fd < 0) { 30 | //client->last_error = IPC_CAPTURE_ERRNO; 31 | //log_errno("socket(2)"); 32 | return -1; 33 | } 34 | 35 | if (connect(fd, (struct sockaddr *) &sock, SUN_LEN(&sock)) < 0) { 36 | //client->last_error = IPC_CAPTURE_ERRNO; 37 | //log_errno("connect(2) to %s", sock.sun_path); 38 | return -1; 39 | } 40 | 41 | return fd; 42 | } 43 | 44 | int main() { 45 | nvlist_t *nvl; 46 | int fd; 47 | int sock; 48 | 49 | sock = setup_client_socket(); 50 | if (sock < 0) err(1, "socket failed"); 51 | 52 | fd = open("/tmp/foo", O_RDONLY); 53 | if (fd < 0) 54 | err(1, "open(\"/tmp/foo\") failed"); 55 | 56 | nvl = nvlist_create(0); 57 | /* 58 | * There is no need to check if nvlist_create() succeeded, 59 | * as the nvlist_add_() functions can cope. 60 | * If it failed, nvlist_send() will fail. 61 | */ 62 | nvlist_add_string(nvl, "command", "hello"); 63 | nvlist_add_string(nvl, "filename", "/tmp/foo"); 64 | nvlist_add_number(nvl, "flags", O_RDONLY); 65 | /* 66 | * We just want to send the descriptor, so we can give it 67 | * for the nvlist to consume (that's why we use nvlist_move 68 | * not nvlist_add). 69 | */ 70 | nvlist_move_descriptor(nvl, "fd", fd); 71 | if (nvlist_send(sock, nvl) < 0) { 72 | nvlist_destroy(nvl); 73 | err(1, "nvlist_send() failed"); 74 | } 75 | nvlist_destroy(nvl); 76 | } 77 | -------------------------------------------------------------------------------- /testing/pingpong-server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../include/ipc.h" 31 | #include "log.h" 32 | 33 | static int server_fd; 34 | 35 | static void 36 | do_cleanup() 37 | { 38 | if (server_fd >= 0) ipc_close(server_fd); 39 | } 40 | 41 | int main(int argc, char *argv[]) { 42 | int result; 43 | int client; 44 | int rv; 45 | uid_t uid; 46 | gid_t gid; 47 | 48 | log_open("pingpong-server", "/dev/stderr"); 49 | 50 | server_fd = ipc_bind(IPC_DOMAIN_USER, "test.ping", 1); 51 | if (server_fd < 0) 52 | errx(1, "bind: %s", ipc_strerror(server_fd)); 53 | atexit(do_cleanup); 54 | 55 | client = ipc_accept(server_fd); 56 | if (client < 0) 57 | errx(1, "accept: %s", ipc_strerror(client)); 58 | 59 | rv = ipc_getpeereid(client, &uid, &gid); 60 | if (rv < 0) 61 | errx(1, "getpeereid: %s", ipc_strerror(rv)); 62 | 63 | log_info("got connection: uid=%d gid=%d", uid, gid); 64 | 65 | if (write(client, "test\0", 5) < 5) 66 | err(1, "write"); 67 | 68 | rv = ipc_close(server_fd); 69 | if (rv < 0) 70 | err(1, "close: %s", ipc_strerror(rv)); 71 | server_fd = -1; 72 | 73 | puts("success"); 74 | exit(EXIT_SUCCESS); 75 | } 76 | -------------------------------------------------------------------------------- /testing/ipcc-1/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../../include/ipc.h" 31 | #include "../../src/log.h" 32 | 33 | int 34 | echo(int *ret1, int arg1) 35 | { 36 | *ret1 = arg1; 37 | return 0; 38 | } 39 | 40 | int main(int argc, char *argv[]) { 41 | struct ipc_server *server; 42 | int result; 43 | int client; 44 | int rv; 45 | int i; 46 | uid_t uid; 47 | gid_t gid; 48 | 49 | setenv("IPC_LIBDIR", "./ipc", 1); 50 | 51 | log_open("server", "/dev/stderr"); 52 | ipc_openlog("server", "/dev/stderr"); 53 | 54 | server = ipc_server(); 55 | if (!server) 56 | errx(1, "ipc_server()"); 57 | 58 | rv = ipc_server_bind(server, IPC_DOMAIN_USER, "com.example.myservice"); 59 | if (rv < 0) 60 | errx(1, "bind: %s", ipc_strerror(rv)); 61 | 62 | /* Do this twice: once to accept a new connection, once to handle the request */ 63 | for (i = 0; i < 2; i++) { 64 | log_info("waiting for event"); 65 | 66 | rv = ipc_server_dispatch(server); 67 | if (rv < 0) 68 | errx(1, "ipc_dispatch: %s", ipc_strerror(rv)); 69 | } 70 | 71 | ipc_server_free(server); 72 | 73 | log_notice("success; exiting normally"); 74 | exit(EXIT_SUCCESS); 75 | } 76 | -------------------------------------------------------------------------------- /doc/ipc_accept.3: -------------------------------------------------------------------------------- 1 | .\" $​Id$ 2 | .\" Copyright (c) 2015, Mark Heily 3 | .\" All rights reserved. 4 | .\" 5 | .\" Redistribution and use in source and binary forms, with or without 6 | .\" modification, are permitted provided that the following conditions are met: 7 | .\" 8 | .\" * Redistributions of source code must retain the above copyright notice, this 9 | .\" list of conditions and the following disclaimer. 10 | .\" 11 | .\" * Redistributions in binary form must reproduce the above copyright notice, 12 | .\" this list of conditions and the following disclaimer in the documentation 13 | .\" and/or other materials provided with the distribution. 14 | .\" 15 | .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | .\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | .\" 26 | .Dd December 26, 2015 27 | .Dt ipc_accept 3 28 | .Os 29 | .Sh NAME 30 | .Nm ipc_accept 31 | .Nd accept a new client for an IPC service 32 | .Sh LIBRARY 33 | libipc, -lipc 34 | .Sh SYNOPSIS 35 | .In ipc.h 36 | .Ft int 37 | .Fn ipc_accept "int s" 38 | .Sh DESCRIPTION 39 | The 40 | .Fn ipc_accept 41 | function accepts a new client connection for an IPC service. 42 | The argument 43 | .Ar s 44 | must have been previously returned via a call to 45 | .Xr ipc_bind 3 46 | . 47 | .Sh RETURN VALUES 48 | A value of -1 is returned if an error occurs; otherwise the return 49 | value is a non-negative integer that is a descriptor for the accepted connection. 50 | .Sh SEE ALSO 51 | .Xr ipc_bind 3 , 52 | .Xr ipc 7 , 53 | .Xr ipcd 8 54 | .Sh AUTHORS 55 | .An Mark Heily 56 | .Mt . -------------------------------------------------------------------------------- /testing/nvlist-experiment/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int setup_listen_socket() 12 | { 13 | struct sockaddr_un name; 14 | char path[PATH_MAX]; 15 | int len; 16 | int sockfd; 17 | 18 | len = snprintf(path, sizeof(path), "%s/foo.sock", "/usr/home/mark/tmp"); 19 | if (len >= sizeof(path) || len < 0) { 20 | err(1, "buffer allocation error"); 21 | } 22 | 23 | name.sun_family = AF_LOCAL; 24 | strncpy(name.sun_path, path, sizeof(name.sun_path)); 25 | 26 | sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); 27 | if (!sockfd) 28 | err(1, "socket"); 29 | 30 | if (bind(sockfd, (struct sockaddr *) &name, SUN_LEN(&name)) < 0) 31 | err(1, "bind"); 32 | 33 | if (listen(sockfd, 1024) < 0) 34 | err(1, "listen"); 35 | 36 | return sockfd; 37 | } 38 | 39 | int accept_connection(int server) 40 | { 41 | struct sockaddr sa; 42 | socklen_t sa_len; 43 | int client; 44 | 45 | client = accept(server, &sa, &sa_len); 46 | if (client < 0) { 47 | //rv = IPC_CAPTURE_ERRNO; 48 | //log_errno("accept(2)"); 49 | return -1; 50 | } 51 | 52 | return client; 53 | } 54 | 55 | int main() { 56 | nvlist_t *nvl; 57 | const char *command; 58 | char *filename; 59 | int fd, client, server; 60 | 61 | server = setup_listen_socket(); 62 | client = accept_connection(server); 63 | if (client < 0) err(1, "bad client"); 64 | 65 | nvl = nvlist_recv(client, 0); 66 | if (nvl == NULL) 67 | err(1, "nvlist_recv() failed"); 68 | 69 | /* For command we take pointer to nvlist's buffer. */ 70 | command = nvlist_get_string(nvl, "command"); 71 | /* 72 | * For filename we remove it from the nvlist and take 73 | * ownership of the buffer. 74 | */ 75 | filename = nvlist_take_string(nvl, "filename"); 76 | /* The same for the descriptor. */ 77 | fd = nvlist_take_descriptor(nvl, "fd"); 78 | 79 | printf("command=%s filename=%s fd=%d", command, filename, fd); 80 | 81 | nvlist_destroy(nvl); 82 | free(filename); 83 | close(fd); 84 | puts("awesome"); 85 | } 86 | -------------------------------------------------------------------------------- /testing/ipcc-2/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Mark Heily 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../../include/ipc.h" 31 | #include "../../src/log.h" 32 | 33 | int pingpong(char **ret1, char *arg1) 34 | { 35 | if (arg1 == NULL || ret1 == NULL) { 36 | puts("WARNING: null argument(s)"); 37 | return -1; 38 | } 39 | if (strcmp(arg1, "ping") == 0) { 40 | *ret1 = strdup("pong"); 41 | } else { 42 | *ret1 = strdup("game over"); 43 | } 44 | return 0; 45 | } 46 | 47 | int main(int argc, char *argv[]) { 48 | struct ipc_server *server; 49 | int result; 50 | int client; 51 | int rv; 52 | uid_t uid; 53 | gid_t gid; 54 | 55 | setenv("IPC_LIBDIR", "./ipc", 1); 56 | log_open("server", "/dev/stderr"); 57 | ipc_openlog("server", "/dev/stderr"); 58 | 59 | server = ipc_server(); 60 | if (!server) 61 | errx(1, "ipc_server()"); 62 | 63 | rv = ipc_server_bind(server, IPC_DOMAIN_USER, "com.example.myservice"); 64 | if (rv < 0) 65 | errx(1, "bind: %s", ipc_strerror(rv)); 66 | 67 | /* Do this twice: once to accept a new connection, once to handle the request */ 68 | for (int i = 0; i < 2; i++) { 69 | log_info("waiting for event"); 70 | 71 | rv = ipc_server_dispatch(server); 72 | if (rv < 0) 73 | errx(1, "ipc_dispatch: %s", ipc_strerror(rv)); 74 | } 75 | 76 | ipc_server_free(server); 77 | 78 | log_notice("success; exiting normally"); 79 | exit(EXIT_SUCCESS); 80 | } 81 | -------------------------------------------------------------------------------- /src/Makefile.OLD: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2015 Mark Heily 3 | # 4 | # Permission to use, copy, modify, and distribute this software for any 5 | # purpose with or without fee is hereby granted, provided that the above 6 | # copyright notice and this permission notice appear in all copies. 7 | # 8 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | # 16 | 17 | include ../install-dir.mk 18 | 19 | libipc_SOURCES=ipc.c log.c fdpass.c 20 | libipc_OBJS=ipc.o log.o fdpass.o 21 | libipc_SONAME=libipc.so.1 22 | libipc_REALNAME=libipc.so.1.0.1 23 | CFLAGS+=-I../include -std=c99 24 | CFLAGS+=-Wall -Werror 25 | 26 | all: $(libipc_REALNAME) libipc.so libipc.so.1 27 | 28 | $(libipc_REALNAME): $(libipc_SOURCES) fdpass.h ipc_private.h log.h 29 | $(CC) -c -fpic -fvisibility=hidden $(CFLAGS) ipc.c 30 | $(CC) -c -fpic -fvisibility=hidden $(CFLAGS) log.c 31 | $(CC) -c -fpic -fvisibility=hidden $(CFLAGS) fdpass.c 32 | $(CC) -shared -fvisibility=hidden -Wl,-soname,$(libipc_SONAME) $(LDFLAGS) \ 33 | -o $(libipc_REALNAME) $(libipc_OBJS) $(LDADD) 34 | # 35 | # KLUDGE: copypasta from above, to build a debug version 36 | # 37 | rm -f $(libipc_OBJS) 38 | $(CC) -c -fpic $(CFLAGS) -g -O0 -DDEBUG ipc.c 39 | $(CC) -c -fpic $(CFLAGS) -g -O0 -DDEBUG log.c 40 | $(CC) -c -fpic $(CFLAGS) -g -O0 -DDEBUG fdpass.c 41 | $(CC) -shared $(LDFLAGS) -o libipc_debug.so $(libipc_OBJS) $(LDADD) 42 | 43 | libipc.so libipc.so.1: 44 | ln -s $(libipc_REALNAME) $@ 45 | 46 | check: $(libipc_REALNAME) 47 | cd ../testing && ./test-harness.sh 48 | 49 | clean: 50 | rm -f *.so *.so.* $(libipc_OBJS) 51 | 52 | install: 53 | $(INSTALL) -s -m 755 $(libipc_REALNAME) $$DESTDIR$(LIBDIR) 54 | $(INSTALL) -ls $(libipc_REALNAME) $$DESTDIR$(LIBDIR)/libipc.so.1 55 | $(INSTALL) -ls $(libipc_REALNAME) $$DESTDIR$(LIBDIR)/libipc.so 56 | $(INSTALL) -m 755 ipcc/ipcc.rb $$DESTDIR$(BINDIR)/ipcc 57 | 58 | uninstall: 59 | rm -f $$DESTDIR$(LIBDIR)/$(libipc_REALNAME) \ 60 | $$DESTDIR$(LIBDIR)/libipc.so.1 \ 61 | $$DESTDIR$(LIBDIR)/libipc.so \ 62 | $$DESTDIR$(BINDIR)/ipcc 63 | 64 | 65 | .PHONY: check install uninstall 66 | -------------------------------------------------------------------------------- /doc/ipc.7: -------------------------------------------------------------------------------- 1 | .\" $​Id$ 2 | .\" Copyright (c) 2015, Mark Heily 3 | .\" All rights reserved. 4 | .\" 5 | .\" Redistribution and use in source and binary forms, with or without 6 | .\" modification, are permitted provided that the following conditions are met: 7 | .\" 8 | .\" * Redistributions of source code must retain the above copyright notice, this 9 | .\" list of conditions and the following disclaimer. 10 | .\" 11 | .\" * Redistributions in binary form must reproduce the above copyright notice, 12 | .\" this list of conditions and the following disclaimer in the documentation 13 | .\" and/or other materials provided with the distribution. 14 | .\" 15 | .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | .\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | .\" 26 | .Dd December 26, 2015 27 | .Dt ipc 7 28 | .Os 29 | .Sh NAME 30 | .Nm ipc 31 | .Nd a mechanism for inter-process communication 32 | .Sh SYNOPSIS 33 | .In ipc.h 34 | .Sh DESCRIPTION 35 | Processes running under a non-privileged user ID may communicate with 36 | .Nm 37 | to request the ability to bind to the IPC namespace. 38 | .Sh API 39 | .Ft "int" 40 | .Fn ipc_accept "int s" ; 41 | .Pp 42 | .Ft "int" 43 | .Fo ipc_bind 44 | .Fa "int domain" 45 | .Fa "const char *service" 46 | .Fa "int version" 47 | .Fc ; 48 | .Pp 49 | .Ft "int" 50 | .Fo ipc_connect 51 | .Fa "int domain" 52 | .Fa "const char *service" 53 | .Fa "int version" 54 | .Fc ; 55 | .Pp 56 | .Ft "int" 57 | .Fo ipc_write 58 | .Fa "int s" 59 | .Fa "const struct iovec *iov" 60 | .Fa "int iovcnt" 61 | .Fc ; 62 | .Pp 63 | .Ft "int" 64 | .Fo ipc_read 65 | .Fa "int s" 66 | .Fa "struct iovec *iov" 67 | .Fa "int *iovcnt" 68 | .Fc ; 69 | .Pp 70 | .Ft "int" 71 | .Fo ipc_close 72 | .Fa "int s" 73 | .Fc ; 74 | .Sh EXIT STATUS 75 | .Ex -std 76 | .Sh ENVIRONMENT 77 | This daemon does not rely on any environment variables. 78 | .Sh AUTHORS 79 | .An Mark Heily 80 | .Mt . -------------------------------------------------------------------------------- /doc/ipc_connect.3: -------------------------------------------------------------------------------- 1 | .\" $​Id$ 2 | .\" Copyright (c) 2015, Mark Heily 3 | .\" All rights reserved. 4 | .\" 5 | .\" Redistribution and use in source and binary forms, with or without 6 | .\" modification, are permitted provided that the following conditions are met: 7 | .\" 8 | .\" * Redistributions of source code must retain the above copyright notice, this 9 | .\" list of conditions and the following disclaimer. 10 | .\" 11 | .\" * Redistributions in binary form must reproduce the above copyright notice, 12 | .\" this list of conditions and the following disclaimer in the documentation 13 | .\" and/or other materials provided with the distribution. 14 | .\" 15 | .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | .\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | .\" 26 | .Dd December 26, 2015 27 | .Dt ipc_connect 3 28 | .Os 29 | .Sh NAME 30 | .Nm ipc_connect 31 | .Nd connect to an IPC service 32 | .Sh LIBRARY 33 | libipc, -lipc 34 | .Sh SYNOPSIS 35 | .In ipc.h 36 | .Ft int 37 | .Fo ipc_connect 38 | .Fa "const char *service" 39 | .Fa "int version" 40 | .Fc 41 | .Sh DESCRIPTION 42 | The 43 | .Fn ipc_connect 44 | function creates a persistent connection to an IPC service. 45 | The service must have been previously registered via 46 | .Xr ipc_bind 3 47 | . 48 | .Pp 49 | It accepts the following arguments: 50 | .Bl -tag -width Ds 51 | .It Fa "const char *service" 52 | The name of the IPC service. 53 | .It Fa "int version" 54 | The ABI version of the client. 55 | .El 56 | .Sh RETURN VALUES 57 | .Rv -std 58 | .Sh SEE ALSO 59 | .Xr ipc_bind 3 , 60 | .Xr ipc 7 , 61 | .Xr ipcd 8 62 | .Sh ERRORS 63 | In addition to the errors returned by the 64 | .Xr bind 2 65 | system call, this function may fail if: 66 | .Pp 67 | .Bl -tag -width Er 68 | .It Bq Er ENOMEM 69 | There is not enough memory to allocate data for the connection. 70 | .El 71 | .Sh AUTHORS 72 | The 73 | .Nm 74 | daemon was written by 75 | .An Mark Heily , 76 | .Mt mark@heily.com . -------------------------------------------------------------------------------- /doc/handbook/examples.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Examples 7 | 8 |
9 | 10 | A simple server 11 | 12 | 13 | The example below shows a simple server for a service called "echo" 14 | that provides a single function named 15 | echo. 16 | 17 | 18 | 19 | This function accepts an argument, and responds with a copy of the 20 | request. 21 | 22 | 23 | 24 | 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | /* Take a line of input and copy it to the output */ 32 | char * 33 | echo(const char *input) 34 | { 35 | return (strdup(input)); 36 | } 37 | 38 | int main(int argc, char *argv[]) 39 | { 40 | int result; 41 | 42 | result = ipc_server_main(IPC_DOMAIN_USER, "echo"); 43 | if (result < 0) 44 | errx(1, "ipc_server_main: %s", ipc_strerror(rv)); 45 | 46 | exit(EXIT_SUCCESS); 47 | } 48 | ]]> 49 | 50 | 51 | 52 | The ipc_server_main starts an IPC server for the local user, and binds to the "echo" service. 53 | It will run in a loop servicing client requests. 54 | 55 |
56 | 57 |
58 | A basic interface definition file 59 | 60 | 61 | Create a file named "echo.ipc" to contain the IPC interface definition. It should look like this: 62 | 63 | 64 | 65 | --- 66 | service: echo 67 | domain: IPC_DOMAIN_USER 68 | functions: 69 | echo: 70 | id: 1 71 | prototype: int echo(int *response, int request) 72 | on_error: NULL 73 | 74 | 75 | 76 | Run ipcc to compile the interface definition file into C code. 77 | 78 | 79 | 80 | $ ipcc echo.ipc 81 | 82 | 83 | 84 | This will generate three files: a skeleton, a stub, and a Makefile fragment. 85 | 86 | 87 |
88 | 89 |
90 | A simple client 91 | 92 | Here is a simple client: 93 | 94 | 95 | 97 | #include 98 | #include 99 | 100 | #include 101 | 102 | int main(int argc, char *argv[]) 103 | { 104 | int result, response; 105 | 106 | result = echo("hello world"); 107 | if (result == NULL) 108 | errx(1, "IPC error: %s", ipc_strerror()); 109 | 110 | puts(result); 111 | 112 | exit(EXIT_SUCCESS); 113 | } 114 | ]]> 115 | 116 | 117 | 118 |
119 | 120 |
121 | Putting it all together 122 | 123 | TODO - this section should describe step-by-step what is happening 124 | under the hood. 125 | 126 |
127 | 128 |
-------------------------------------------------------------------------------- /src/ipc-config/ipc-config.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2016 Mark Heily 4 | # 5 | # Permission to use, copy, modify, and distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | # 17 | 18 | 19 | usage() { 20 | cat << EOF 21 | usage: ipc-config [MODE] [OPTION] [IPC SERVICES] 22 | 23 | mode is one of: 24 | --client output information to build IPC clients 25 | --server output information to build IPC servers 26 | 27 | option is one of: 28 | --cflags print the compiler flags 29 | --ldflags print the linker flags 30 | --ldadd print additional linker objects 31 | 32 | For more information, visit . 33 | EOF 34 | } 35 | 36 | mode=$1 37 | shift 38 | option=$1 39 | shift 40 | 41 | if [ -z "$option" -o -z "$mode" ] ; then 42 | echo "ERROR: required parameters and