1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/* daemon.c: This is -*- c -*-
Implements the functions used in talking to the user-defaults daemon
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include "proplist.h"
#include "plconf.h"
#include "util.h"
#define GIVEUP(x, y) { char buf[255]; \
fprintf(stderr, "libPropList: %s:\n", x); \
sprintf(buf, "libPropList: %s", y); \
perror(buf); \
fprintf(stderr, "libPropList: Giving up.\n"); \
exit(1); }
static int sock;
static pid_t mypid, childpid;
static plcallback_t cb;
static char password[255];
BOOL initialized = NO;
void sighandler(int sig)
{
#if 0
signal(sig, sighandler);
#else
/* in Linux, signals are reset to their default behaviour after raised.
* Since it is quite common that libPL raises many HUPs quickly... */
struct sigaction sig_action;
sig_action.sa_handler = sighandler;
sigemptyset(&sig_action.sa_mask);
sigaction(SIGHUP, &sig_action, NULL);
#endif
if(cb)
(*cb)();
}
int start_daemon(void)
{
char *daemonpath;
daemonpath = ManglePath(DAEMON);
if((childpid = fork()) < 0)
return -1;
if(childpid==0)
{
/* execute daemon */
if(execvp(daemonpath, NULL) < 0)
{
fprintf(stderr, "libPropList: Couldn't start daemon %s:\n", DAEMON);
perror("libPropList: start_daemon");
fprintf(stderr, "libPropList: Giving up.\n");
kill(mypid, SIGTERM);
exit(1);
}
}
free(daemonpath);
return 0;
}
void initialize(void)
{
struct stat file_stat;
FILE *pidfile;
char *pidfilename;
char buf[255];
int portno;
pid_t pid;
int i;
mypid = getpid();
pidfilename = ManglePath(PIDFILE);
if(stat(pidfilename, &file_stat) < 0)
if(start_daemon() < 0)
{
fprintf(stderr, "libPropList: Could not start daemon %s:\n", DAEMON);
perror("libPropList: start_daemon");
fprintf(stderr, "libPropList: Giving up.\n");
exit(1);
}
else /* start_daemon succeeded */
if(stat(pidfilename, &file_stat) < 0)
{
i=0;
while(i<TIMEOUT)
{
sleep(1);
i++;
if(stat(pidfilename, &file_stat) == 0) /* success */
goto gotit;
}
fprintf(stderr, "libPropList: Could not start daemon %s: Timeout. Giving up.\n", DAEMON);
kill(childpid, SIGTERM);
exit(1);
}
gotit:
pidfile = fopen(pidfilename, "r");
if(!pidfile)
{
GIVEUP("Could not open PID file.", "fopen");
kill(childpid, SIGTERM);
exit(1);
}
fscanf(pidfile, "%d %d %s", &pid, &portno, password);
sock = GetClientSocket(portno);
if(sock<0)
GIVEUP("Couldn't initiate connection", "GetClientSocket");
sprintf(buf, "auth %s\n", password);
if(!(WriteString(sock, buf)))
GIVEUP("Couldn't authorize myself!", "auth");
initialized = YES;
free(pidfilename);
}
proplist_t PLGetDomainNames()
{
char *desc;
proplist_t arr;
if(!initialized) initialize();
if(!(WriteString(sock, "list\n")))
return NULL;
if(!(desc = ReadStringAnySize(sock)))
return NULL;
arr = PLGetProplistWithDescription(desc);
MyFree(__FILE__, __LINE__, desc);
return arr;
}
proplist_t PLGetDomain(proplist_t name)
{
char *desc, *str;
proplist_t domain;
if(!initialized) initialize();
desc = PLGetDescription(name);
str = (char *)MyMalloc(__FILE__, __LINE__, strlen(desc)+6);
sprintf(str, "get %s\n", desc);
MyFree(__FILE__, __LINE__, desc);
if(!(WriteString(sock, str)))
{
MyFree(__FILE__, __LINE__, str);
return NULL;
}
MyFree(__FILE__, __LINE__, str);
if(!(desc = ReadStringAnySize(sock)))
return NULL;
if(!strcmp(desc, "nil"))
{
MyFree(__FILE__, __LINE__, desc);
return NULL;
}
domain = PLGetProplistWithDescription(desc);
MyFree(__FILE__, __LINE__, desc);
return domain;
}
proplist_t PLSetDomain(proplist_t name, proplist_t value,
BOOL kickme)
{
char *name_desc, *value_desc;
char *str;
if(!initialized) initialize();
name_desc = PLGetDescription(name);
value_desc = PLGetDescription(value);
str = (char *)MyMalloc(__FILE__, __LINE__, strlen(name_desc)+strlen(value_desc)+50);
if(kickme)
sprintf(str, "set %s %s\n", name_desc, value_desc);
else
sprintf(str, "set-nonotify %d %s %s\n", mypid, name_desc,
value_desc);
MyFree(__FILE__, __LINE__, name_desc);
MyFree(__FILE__, __LINE__, value_desc);
if(!(WriteString(sock, str)))
{
MyFree(__FILE__, __LINE__, str);
return NULL;
}
MyFree(__FILE__, __LINE__, str);
return(value);
}
proplist_t PLDeleteDomain(proplist_t name, BOOL kickme)
{
char *name_desc;
char *str;
if(!initialized) initialize();
name_desc = PLGetDescription(name);
str = (char *)MyMalloc(__FILE__, __LINE__, strlen(name_desc)+50);
if(kickme)
sprintf(str, "remove %s\n", name_desc);
else
sprintf(str, "remove-nonotify %d %s\n", mypid, name_desc);
MyFree(__FILE__, __LINE__, name_desc);
if(!(WriteString(sock, str)))
{
MyFree(__FILE__, __LINE__, str);
return NULL;
}
MyFree(__FILE__, __LINE__, str);
return name;
}
proplist_t PLRegister(proplist_t name, plcallback_t callback)
{
char *str, *desc;
if(!initialized) initialize();
cb = callback;
signal(SIGNAL, &sighandler);
if(name)
{
desc = PLGetDescription(name);
str = (char *)MyMalloc(__FILE__, __LINE__, strlen(desc)+50);
sprintf(str, "register %d %s\n", mypid, desc);
MyFree(__FILE__, __LINE__, desc);
}
else
{
str = (char *)MyMalloc(__FILE__, __LINE__, 50);
sprintf(str, "register %d\n", mypid);
}
if(!(WriteString(sock, str)))
{
MyFree(__FILE__, __LINE__, str);
return NULL;
}
MyFree(__FILE__, __LINE__, str);
return(name);
}
proplist_t PLUnregister(proplist_t name)
{
char *str, *desc;
if(!initialized) initialize();
signal(SIGNAL, &sighandler);
if(name)
{
desc = PLGetDescription(name);
str = (char *)MyMalloc(__FILE__, __LINE__, strlen(desc)+50);
sprintf(str, "unregister %d %s\n", mypid, desc);
MyFree(__FILE__, __LINE__, desc);
}
else
{
str = (char *)MyMalloc(__FILE__, __LINE__, 50);
sprintf(str, "unregister %d\n", mypid);
cb = NULL;
signal(SIGNAL, SIG_DFL);
}
if(!(WriteString(sock, str)))
{
MyFree(__FILE__, __LINE__, str);
return NULL;
}
MyFree(__FILE__, __LINE__, str);
return(name);
}
|