1 /***************************************************************************
2 kdbd.c - The server for the daemon backend
4 copyright : (C) 2006 by Yannick Lecaillez
5 email : sizon5@gmail.com
6 ***************************************************************************/
8 /***************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the BSD License (revised). *
13 ***************************************************************************/
26 #include "kdbbackend.h"
30 #include "kdb_wrapper.h"
34 static Message *processRequest(Message *request, KDB *handle, uid_t remoteeuid, gid_t remoteegid);
36 static Message *processRequest(Message *request, KDB *handle, uid_t remoteeuid, gid_t remoteegid)
39 int msgType, procedure;
41 msgType = messageGetType(request);
42 if ( msgType != MESSAGE_REQUEST ) {
43 fprintf(stderr, "processRequest(): Received a non-request message.\n");
47 procedure = messageGetProcedure(request);
50 reply = wrapper_kdbOpen(handle, request, remoteeuid, remoteegid);
54 reply = wrapper_kdbClose(handle, request);
58 reply = wrapper_kdbStatKey(handle, request);
62 reply = wrapper_kdbGetKey(handle, request);
66 reply = wrapper_kdbSetKey(handle, request);
70 reply = wrapper_kdbSetKeys(handle, request);
74 reply = wrapper_kdbGetChild(handle, request);
81 if ( reply == NULL ) {
82 /* Internat error from wrapper */
83 fprintf(stderr, "Internal error\n");
84 reply = messageNew(MESSAGE_REPLY, INTERNAL_ERROR,
85 DATATYPE_INTEGER, &errno,
88 fprintf(stderr, "An internal error caused a fatal error. Client will be confused :-(.\n");
94 int kdbd(void *pIntThreadHandle)
97 int threadHandle, socketFd;
98 Message *request, *reply;
106 pthread_cleanup_push(threadExit, pIntThreadHandle);
108 threadHandle = *((int *) pIntThreadHandle);
110 if ( (socketFd = threadGetSocket(threadHandle)) == -1 ) {
111 fprintf(stderr, "Can't get socket :-(\n");
115 if ( ipc_eid(socketFd, &remoteeuid, &remoteegid, &remotepid) == -1 ) {
119 fprintf(stderr, "Thread %ld launched to serve PID %d (euid=%d/egid=%d)\n", pthread_self(), remotepid, remoteeuid, remoteegid);
123 request = protocolReadMessage(socketFd);
124 if ( request == NULL ) {
125 if ( (errno == EPIPE) || (errno == EINVAL) ) {
126 /* Client closed the connection or
127 * malformed request */
131 /* They are probably some usefull errno
137 closed = (messageGetProcedure(request) == KDB_BE_CLOSE);
139 reply = processRequest(request, &handle, remoteeuid, remoteegid);
142 protocolSendMessage(socketFd, reply);
146 pthread_cleanup_pop(1);