From 0de4466f7cefb55584b24589acaca2284fea8d24 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 28 Dec 2015 01:43:39 +0100 Subject: fix errors and tidy up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/satd.c | 60 +++++++++++------------------------------------------------- 1 file changed, 11 insertions(+), 49 deletions(-) (limited to 'src/satd.c') diff --git a/src/satd.c b/src/satd.c index c2c1525..ec0d205 100644 --- a/src/satd.c +++ b/src/satd.c @@ -19,56 +19,21 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#include -#include -#include +#include #include -#include #include #include +#include #include "daemonise.h" +#include "common.h" -#define t(...) do { if (__VA_ARGS__) goto fail; } while (0) +COMMAND("satd") +USAGE("[-f]") -/** - * The name of the process. - */ -char *argv0 = "satd"; - - - -/** - * Print usage information. - */ -static void -usage(void) -{ - fprintf(stderr, "usage: [-f] %s\n", - strrchr(argv0) ? (strrchr(argv0) + 1) : argv0); - exit(2); -} - - -/** - * Get the group ID of a group. - * - * @param name The name of the group. - * @param fallback The group ID to return if the group is not found. - * @return The ID of the group, -1 on error. - */ -static gid_t -getgroup(const char* name, gid_t fallback) -{ - struct group *g; - if (!(g = getgrnam(name))) - return errno ? (gid_t)-1 : fallback; - return g->gr_gid; -} - /** * Create the socket. @@ -82,22 +47,19 @@ create_socket(struct sockaddr_un *address) int fd = -1; ssize_t len; char *dir; - gid_t group; + int saved_errno; - dir = getenv("XDG_RUNTIME_DIR"), dir = (dir ? dir : "/run") + dir = getenv("XDG_RUNTIME_DIR"), dir = (dir ? dir : "/run"); t (snprintf(NULL, 0, "%s/satd.socket%zn", dir, &len) == -1); - if ((len < 0) || (len >= sizeof(address->sun_path))) { - errno = ENAMETOOLING; - goto fail; - } - sprintf(address->sun_path, "%s/satd.socket", dir) + if ((len < 0) || ((size_t)len >= sizeof(address->sun_path))) + t ((errno = ENAMETOOLONG)); + sprintf(address->sun_path, "%s/satd.socket", dir); address->sun_family = AF_UNIX; /* TODO test flock */ unlink(address->sun_path); t ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1); t (fchmod(fd, S_IRWXU) == -1); - t ((group = getgroup("nobody", 0)) == -1); - t (fchown(fd, getuid(), group) == -1); + t (fchown(fd, getuid(), getgid()) == -1); t (bind(fd, (struct sockaddr *)address, sizeof(*address)) == -1); /* TODO flock */ -- cgit v1.2.3-70-g09d2