Subversion Repositories heizung

Rev

Rev 8 | Rev 10 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8 Rev 9
Line 5... Line 5...
5
 
5
 
6
#include <stdio.h>
6
#include <stdio.h>
7
#include <string.h>
7
#include <string.h>
8
#include <unistd.h>
8
#include <unistd.h>
9
#include <stdlib.h>
9
#include <stdlib.h>
-
 
10
#include <libgen.h>
10
#include <math.h>
11
#include <math.h>
11
#include <time.h>
12
#include <time.h>
12
#include <signal.h>
13
#include <signal.h>
13
#include <syslog.h>
14
#include <syslog.h>
14
#include <errno.h>
15
#include <errno.h>
Line 16... Line 17...
16
#include <sys/stat.h>
17
#include <sys/stat.h>
17
#include <sys/types.h>
18
#include <sys/types.h>
18
#include <sys/socket.h>
19
#include <sys/socket.h>
19
#include <fcntl.h>
20
#include <fcntl.h>
20
#include <netdb.h>
21
#include <netdb.h>
-
 
22
#include <dirent.h>
21
#include <netinet/in.h>
23
#include <netinet/in.h>
22
#include <sys/param.h>
24
#include <sys/param.h>
23
#include <sys/ioctl.h>
25
#include <sys/ioctl.h>
24
#include <pwd.h>
26
#include <pwd.h>
25
#include <grp.h>
27
#include <grp.h>
Line 39... Line 41...
39
{
41
{
40
      HEIZUNG *heizung;
42
      HEIZUNG *heizung;
41
      struct HEIZINDEX *next;
43
      struct HEIZINDEX *next;
42
}HEIZINDEX;
44
}HEIZINDEX;
43
 
45
 
-
 
46
struct SOCKETS
-
 
47
{
-
 
48
      int sockfd;
-
 
49
      int newfd;
-
 
50
};
-
 
51
 
44
CONFIGURE configs;
52
CONFIGURE configs;
45
HEIZINDEX *HeizFirst;
53
HEIZINDEX *HeizFirst;
46
float ActTemperature;
54
float ActTemperature;
47
float ActPressure;
55
float ActPressure;
48
 
56
 
49
static pthread_t pthr_pars, pthr_temp_pars;
57
static pthread_t pthr_pars, pthr_temp_pars, pthr_process;
50
 
58
 
51
// Prototypes
59
// Prototypes
52
void daemon_start(int ignsigcld);
60
void daemon_start(int ignsigcld);
53
void *pthr_temperature(void *pV_data);
61
void *pthr_temperature(void *pV_data);
54
void *pthr_parser(void *pV_data);
62
void *pthr_parser(void *pV_data);
55
void sig_child(void);
63
void sig_child(void);
-
 
64
void sendList(int s1);
-
 
65
int listSchemas(int s1);
56
int parseCommand(int s1, char *cmd);
66
int parseCommand(int s1, char *cmd);
-
 
67
void *processCommands(void *pV_data);
57
void changeToUser(char *, char *);
68
void changeToUser(char *, char *);
58
 
69
 
59
HEIZINDEX *allocateMemory(void);
70
HEIZINDEX *allocateMemory(void);
60
HEIZINDEX *insertMemory(HEIZINDEX *pos);
71
HEIZINDEX *insertMemory(HEIZINDEX *pos);
61
void freeMemory(void);
72
void freeMemory(void);
62
HEIZINDEX *freeChainMember(HEIZINDEX *member);
73
HEIZINDEX *freeChainMember(HEIZINDEX *member);
63
void freeDay(int wday);
74
void freeDay(int wday);
64
void insertMember(int wday, long endt, float temp);
75
void insertMember(int wday, long endt, float temp);
-
 
76
char *makeFileName(char *ret, char *fname, int len);
65
int readHeizPlan(void);
77
int readHeizPlan(void);
-
 
78
int readHeizPlanName(char *fname);
66
int writeHeizPlan(void);
79
int writeHeizPlan(void);
-
 
80
int writeHeizPlanName(char *fname);
67
void readConf(void);
81
void readConf(void);
68
 
82
 
69
char *readLine(int fd, char *buf, int bufLen);
83
char *readLine(int fd, char *buf, int bufLen);
70
char *trim(char *str);
84
char *trim(char *str);
71
char *remove_string(char *str, char *search, char *ret);
85
char *remove_string(char *str, char *search, char *ret);
72
 
86
 
73
static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
87
static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
74
static pthread_mutex_t fastmutex_ser = PTHREAD_MUTEX_INITIALIZER;
88
static pthread_mutex_t fastmutex_ser = PTHREAD_MUTEX_INITIALIZER;
-
 
89
static pthread_mutex_t fastmutex_proc = PTHREAD_MUTEX_INITIALIZER;
75
 
90
 
76
/*
91
/*
77
 * The main program, initializing everything and start the daemon.
92
 * The main program, initializing everything and start the daemon.
78
 */
93
 */
79
int main(int argc, char *argv[])
94
int main(int argc, char *argv[])
Line 278... Line 293...
278
	      sz.tm_mon = zeit->tm_mon;
293
	      sz.tm_mon = zeit->tm_mon;
279
	      sz.tm_mday = zeit->tm_mday;
294
	      sz.tm_mday = zeit->tm_mday;
280
	      sz.tm_year = zeit->tm_year;
295
	      sz.tm_year = zeit->tm_year;
281
	      sz.tm_isdst = zeit->tm_isdst;
296
	      sz.tm_isdst = zeit->tm_isdst;
282
	      tstart = mktime(&sz);
297
	      tstart = mktime(&sz);
283
	      loctm = t - tstart;	// Seconds since midnight
298
	      loctm = (t - tstart) + 1;	// Seconds since midnight
284
	      act = HeizFirst;
299
	      act = HeizFirst;
285
 
300
 
286
	      while(act)
301
	      while(act)
287
	      {
302
	      {
288
		 if (act->heizung->wday == wday && loctm >= act->heizung->start && loctm <= act->heizung->end)
303
		 if (act->heizung->wday == wday && loctm >= act->heizung->start && loctm <= act->heizung->end)
Line 309... Line 324...
309
	}
324
	}
310
 
325
 
311
	pthread_mutex_unlock(&fastmutex_ser);
326
	pthread_mutex_unlock(&fastmutex_ser);
312
}
327
}
313
 
328
 
-
 
329
void *processCommands(void *pV_data)
-
 
330
{
-
 
331
char ch, buf[128];
-
 
332
int i, s1, s;
-
 
333
struct SOCKETS *soc;
-
 
334
 
-
 
335
	soc = (struct SOCKETS *)pV_data;
-
 
336
	s1 = soc->newfd;
-
 
337
	s = soc->sockfd;
-
 
338
	memset(&buf[0], 0, sizeof(buf));
-
 
339
	i = 0;
-
 
340
 
-
 
341
	while (read(s1,&ch,1) > 0)
-
 
342
	{
-
 
343
	   if (i < (sizeof(buf) - 1))
-
 
344
	   {
-
 
345
	      buf[i] = ch;
-
 
346
 
-
 
347
	      if (ch == ';' || ch == 0x0d)
-
 
348
	      {
-
 
349
	      int pstat;
-
 
350
 
-
 
351
		 pthread_mutex_lock (&fastmutex_proc);
-
 
352
 
-
 
353
		 if (!strncmp(buf, "quit", 4))
-
 
354
		    break;
-
 
355
 
-
 
356
		 if ((pstat = parseCommand(s1,buf)) == 0)
-
 
357
		 {
-
 
358
		 char hv0[128];
-
 
359
 
-
 
360
		    sprintf(&hv0[0],"INVALID COMMAND: %s;",buf);
-
 
361
		    write(s1,hv0,strlen(hv0));
-
 
362
		 }
-
 
363
		 else if (pstat == 2)
-
 
364
		    write(s1, "NAK;", 4);
-
 
365
		 else
-
 
366
		    write(s1,"OK;",3);
-
 
367
 
-
 
368
		 memset(&buf[0], 0, sizeof(buf));
-
 
369
		 i = 0;
-
 
370
		 pthread_mutex_unlock(&fastmutex_proc);
-
 
371
		 continue;
-
 
372
	      }
-
 
373
	   }
-
 
374
 
-
 
375
	   i++;
-
 
376
	}
-
 
377
 
-
 
378
	close(s1);
-
 
379
}
-
 
380
 
314
void *pthr_parser( void *pV_data )
381
void *pthr_parser(void *pV_data)
315
{
382
{
316
int i;
-
 
317
char ch, buf[128], str[INET_ADDRSTRLEN];
383
char ch, str[INET_ADDRSTRLEN];
318
// socket structure
384
// socket structure
319
struct sockaddr_in client1, server1;
385
struct sockaddr_in client1, server1;
320
struct servent *serviceInfo;
386
struct servent *serviceInfo;
321
int s1,s;
387
int s1, s;
322
socklen_t length;
388
socklen_t length;
-
 
389
struct SOCKETS soc;
323
 
390
 
324
	// socket server
391
	// socket server
325
	if (configs.port > 0)
392
	if (configs.port > 0)
326
	   server1.sin_port = htons(configs.port);
393
	   server1.sin_port = htons(configs.port);
327
	else if ((serviceInfo = getservbyname ("heizung", "tcp")))
394
	else if ((serviceInfo = getservbyname ("heizung", "tcp")))
Line 357... Line 424...
357
	length = sizeof(client1);
424
	length = sizeof(client1);
358
        syslog (LOG_DEBUG, "Server ready: %d",s);
425
        syslog (LOG_DEBUG, "Server ready: %d",s);
359
 
426
 
360
	while (1)
427
	while (1)
361
	{
428
	{
-
 
429
	   pthread_mutex_lock (&fastmutex);
362
	   // Read time table at every loop, to be sure to have the latest
430
	   // Read time table at every loop, to be sure to have the latest
363
	   // version (User may change it at every time)
431
	   // version (User may change it at every time)
364
	   if (!readHeizPlan())
432
	   if (!readHeizPlan())
365
	   {
433
	   {
366
	      if (!HeizFirst)
434
	      if (!HeizFirst)
367
	      {
435
	      {
368
	         syslog(LOG_DAEMON, "Error reading table %s with plan!", configs.HeizPath);
436
		 syslog(LOG_DAEMON, "Error reading table %s with plan!", configs.HeizPath);
369
	         exit(EXIT_FAILURE);
437
		 exit(EXIT_FAILURE);
370
	      }
438
	      }
371
	      else
439
	      else
372
		 syslog(LOG_DAEMON, "Error reading table with plan! Will work with old one.");
440
		 syslog(LOG_DAEMON, "Error reading table with plan! Will work with old one.");
373
	   }
441
	   }
374
 
442
 
-
 
443
	   pthread_mutex_unlock(&fastmutex);
-
 
444
 
375
	   if ((s1 = accept (s, (struct sockaddr *)&client1, &length)) < 0)
445
	   if ((s1 = accept (s, (struct sockaddr *)&client1, &length)) < 0)
-
 
446
	   {
376
	      syslog (LOG_DAEMON, "Error in accept: %d: %s", s1, strerror(errno));
447
	      syslog (LOG_DAEMON, "Error in accept: %d: %s", s1, strerror(errno));
-
 
448
	      continue;
-
 
449
	   }
377
 
450
 
378
	   pthread_mutex_lock (&fastmutex);
-
 
379
	   inet_ntop(AF_INET, &(client1.sin_addr), str, INET_ADDRSTRLEN);
451
	   inet_ntop(AF_INET, &(client1.sin_addr), str, INET_ADDRSTRLEN);
380
	   syslog (LOG_INFO, "Connected to client %s", str);
452
	   syslog (LOG_INFO, "Connected to client %s", str);
381
	   memset(&buf[0], 0, sizeof(buf));
453
	   soc.sockfd = s;
382
	   i = 0;
454
	   soc.newfd = s1;
383
 
455
 
384
	   while (read(s1,&ch,1) > 0)
456
	   if (pthread_create(&pthr_process, NULL, processCommands, (void *)&soc) != 0)
385
	   {
457
	   {
-
 
458
	      syslog (LOG_DAEMON,"Create of thread \"pthr_parser\" failed!");
386
	      if (i < (sizeof(buf) - 1))
459
	      close(s1);
387
	      {
460
	   }
388
		 buf[i] = ch;
461
	}
389
 
462
 
390
		 if (ch == ';' || ch == 0x0d)
-
 
391
		 {
463
	close (s);
392
		    if (!strncmp(buf, "quit", 4))
464
	serial_close();
393
		       break;
465
	return NULL;
-
 
466
}
394
 
467
 
395
		    if (!parseCommand(s1,buf))
468
void sendList(int s1)
396
		    {
469
{
397
		    char hv0[128];
470
char hv0[256];
-
 
471
int i, wday = 1;
-
 
472
HEIZINDEX *act;
398
 
473
 
399
		       sprintf(&hv0[0],"INVALID COMMAND: %s;",buf);
474
	// We will write out the week days in the correct order.
400
		       write(s1,hv0,strlen(hv0));
475
	// So we need 2 loops. The first one is runnung for every week day
-
 
476
	// and the 2nd one will find every entry for the actual week day.
-
 
477
	while (wday <= 7)
401
		    }
478
	{
402
		    else
479
	   act = HeizFirst;
403
		       write(s1,"OK;",3);
480
	   i = 1;		// The line counter --> reset it to 1 for every week day
404
 
481
 
-
 
482
	   while (act)
-
 
483
	   {
405
		    memset(&buf[0], 0, sizeof(buf));
484
	      if (act->heizung->wday == wday)
406
		    i = 0;
485
	      {
-
 
486
		 sprintf(&hv0[0], "LINE:%d:%d:%lu:%lu:%.1f;", i, act->heizung->wday,
-
 
487
			act->heizung->start, act->heizung->end, act->heizung->temp);
407
		    continue;
488
		 write(s1, &hv0[0], strlen(hv0));
408
		 }
489
		 i++;
409
	      }
490
	      }
410
 
491
 
411
	      i++;
492
	      act = act->next;
412
	   }
493
	   }
413
 
494
 
414
	   close (s1);
495
	   wday++;
415
	   pthread_mutex_unlock(&fastmutex);
-
 
416
	}
496
	}
-
 
497
}
417
 
498
 
-
 
499
int listSchemas(int s1)
-
 
500
{
-
 
501
char hv0[256];
-
 
502
DIR *dir;	
-
 
503
struct dirent *de;
-
 
504
char *ldup, *lp;
-
 
505
 
-
 
506
	if ((ldup = strdup(configs.HeizPath)) == NULL)
-
 
507
	{
-
 
508
	   syslog(LOG_DAEMON, "Not enough memory for path: %s", strerror(errno));
-
 
509
	   return 2;
-
 
510
	}
-
 
511
 
-
 
512
	lp = dirname(ldup);
-
 
513
 
-
 
514
	if ((dir = opendir(lp)) == NULL)
-
 
515
	{
-
 
516
	   syslog(LOG_DAEMON, "Could not open directory %s: %s", lp, strerror(errno));
418
	serial_close();
517
	   free(ldup);
-
 
518
	   return 2;
-
 
519
	}
-
 
520
 
-
 
521
	strcpy(&hv0[0], "SCHEMA START;");
-
 
522
	write(s1, hv0, strlen(hv0));
-
 
523
 
-
 
524
	while ((de = readdir(dir)) != NULL)
-
 
525
	{
-
 
526
	   if (de->d_type == DT_REG && strstr(de->d_name, ".heat") != NULL)
-
 
527
	   {
-
 
528
	   char dn[256], *p;
-
 
529
 
-
 
530
	      memset(&dn[0], 0, sizeof(dn));
-
 
531
	      strncpy(dn, de->d_name, sizeof(dn)-1);
-
 
532
 
-
 
533
	      if ((p = strrchr(dn, '.')) != NULL)
-
 
534
		 *p = 0;
-
 
535
	      else
-
 
536
		 continue;
-
 
537
 
-
 
538
	      sprintf(&hv0[0], "SCHEMA FILE:%s;", dn);
-
 
539
	      write(s1, hv0, strlen(hv0));
-
 
540
	   }
-
 
541
	}
-
 
542
 
-
 
543
	closedir(dir);
-
 
544
	free(ldup);
419
	return NULL;
545
	return 1;
420
}
546
}
421
 
547
 
422
int parseCommand(int s1, char *cmd)
548
int parseCommand(int s1, char *cmd)
423
{
549
{
424
char bef[32],par[1024], *p;
550
char bef[32],par[1024], *p;
Line 430... Line 556...
430
	memset(par,0,sizeof(par));
556
	memset(par,0,sizeof(par));
431
 
557
 
432
	if ((p = strchr(cmd,':')) != NULL)		// do we have a parameter?
558
	if ((p = strchr(cmd,':')) != NULL)		// do we have a parameter?
433
	{
559
	{
434
	   strncpy(bef,cmd,p - cmd);
560
	   strncpy(bef,cmd,p - cmd);
435
	   strncpy(par,p+1,strlen(p+1));
561
	   strncpy(par,p+1,strlen(p)-2);	// Cut off the trailing semi colon
436
	}
562
	}
437
	else
563
	else
438
	   strncpy(bef,cmd,strlen(cmd)-1);
564
	   strncpy(bef,cmd,strlen(cmd)-1);
439
 
565
 
440
	if (!strcasecmp(bef, "LIST"))		// Write out current list
566
	if (!strcasecmp(bef, "LIST"))		// Write out current list
441
	{
-
 
442
	   int wday = 1;
-
 
443
 
-
 
444
	   i = 1;	// The line counter
-
 
445
	   // We will write out the week days in the correct order.
-
 
446
	   // So we need 2 loops. The first one is runnung for every week day
-
 
447
	   // and the 2nd one will find every entry for the actual week day.
-
 
448
	   while (wday <= 7)
-
 
449
	   {
-
 
450
	      act = HeizFirst;
-
 
451
 
-
 
452
	      while (act)
567
	   sendList(s1);
453
	      {
-
 
454
		 if (act->heizung->wday == wday)
-
 
455
		 {
-
 
456
		    sprintf(&hv0[0], "LINE:%d:%d:%lu:%lu:%.1f;", i, act->heizung->wday,
-
 
457
			act->heizung->start, act->heizung->end, act->heizung->temp);
-
 
458
		    write(s1, &hv0[0], strlen(hv0));
-
 
459
		    i++;
-
 
460
		 }
-
 
461
 
-
 
462
		 act = act->next;
-
 
463
	      }
-
 
464
 
-
 
465
	      wday++;
-
 
466
	   }
-
 
467
	}
-
 
468
 
568
 
469
	if (!strcasecmp(bef, "GET WDAY"))		// Write out a particular week day
569
	if (!strcasecmp(bef, "GET WDAY"))		// Write out a particular week day
470
	{
570
	{
471
	int wday = atoi(par);
571
	int wday = atoi(par);
472
 
572
 
Line 549... Line 649...
549
		 if ((act = allocateMemory()) == NULL)
649
		 if ((act = allocateMemory()) == NULL)
550
		 {
650
		 {
551
		    syslog(LOG_DAEMON,"Error allocating memory for a temperature line.");
651
		    syslog(LOG_DAEMON,"Error allocating memory for a temperature line.");
552
		    sprintf(&hv0[0], "ERROR:Not enough memory!");
652
		    sprintf(&hv0[0], "ERROR:Not enough memory!");
553
		    write(s1, &hv0[0], strlen(hv0));
653
		    write(s1, &hv0[0], strlen(hv0));
554
		    return 0;
654
		    return 2;
555
		 }
655
		 }
556
 
656
 
557
		 act->heizung->wday = wday;
657
		 act->heizung->wday = wday;
558
		 act->heizung->end = endt;
658
		 act->heizung->end = endt;
559
		 act->heizung->temp = temp;
659
		 act->heizung->temp = temp;
Line 584... Line 684...
584
	   tmp = atof(par);
684
	   tmp = atof(par);
585
	   insertMember(wday, endt, tmp);
685
	   insertMember(wday, endt, tmp);
586
	   writeHeizPlan();
686
	   writeHeizPlan();
587
	}
687
	}
588
 
688
 
-
 
689
	// SAVE SCHEMA:<name>;
-
 
690
	if (!strcasecmp(bef, "SAVE SCHEMA"))	// Save the current heating plan into a file
-
 
691
        {
-
 
692
	   sprintf(&hv0[0], "%s.heat", par);
-
 
693
 
-
 
694
	   if (!writeHeizPlanName(hv0))
-
 
695
	      return 2;
-
 
696
	}
-
 
697
 
-
 
698
	// LOAD SCHEMA:<name>;
-
 
699
	if (!strcasecmp(bef, "LOAD SCHEMA"))	// Load a previous saved schema from a file
-
 
700
	{
-
 
701
	   sprintf(&hv0[0], "%s.heat", par);
-
 
702
 
-
 
703
	   if (!readHeizPlanName(hv0))
-
 
704
	      return 2;
-
 
705
 
-
 
706
	   // write the new schema immediately to the default file
-
 
707
	   writeHeizPlan();
-
 
708
	   sendList(s1);
-
 
709
	}
-
 
710
 
-
 
711
	// DELETE SCHEMA:<name>;
-
 
712
	if (!strcasecmp(bef, "DELETE SCHEMA"))	// Delete an existing schema
-
 
713
        {
-
 
714
	   makeFileName(&hv0[0], par, sizeof(hv0));
-
 
715
	   
-
 
716
	   if (strlen(hv0) < (sizeof(hv0) - 6))
-
 
717
	      strcat(&hv0[0], ".heat");
-
 
718
	   else
-
 
719
	   {
-
 
720
	      syslog(LOG_DAEMON, "File name too large for internal buffer");
-
 
721
	      return 2;
-
 
722
	   }
-
 
723
 
-
 
724
	   if (access(hv0, R_OK | W_OK))
-
 
725
	   {
-
 
726
	      syslog(LOG_DAEMON,"No access to file %s: %s", hv0, strerror(errno));
-
 
727
	      return 2;
-
 
728
	   }
-
 
729
 
-
 
730
	   if (unlink(hv0) == -1)
-
 
731
	   {
-
 
732
	      syslog(LOG_DAEMON,"Error deleting file %s: %s", hv0, strerror(errno));
-
 
733
	      return 2;
-
 
734
	   }
-
 
735
 
-
 
736
	   return listSchemas(s1);
-
 
737
	}
-
 
738
 
-
 
739
	// LIST SCHEMA;
-
 
740
	if (!strcasecmp(bef, "LIST SCHEMA"))	// Return a list of all available schamas
-
 
741
	   return listSchemas(s1);
-
 
742
 
589
	return 1;
743
	return 1;
590
}
744
}
591
 
745
 
592
/*
746
/*
593
 * Remove a complete day
747
 * Remove a complete day
Line 920... Line 1074...
920
	}
1074
	}
921
 
1075
 
922
	close (fd);
1076
	close (fd);
923
}
1077
}
924
 
1078
 
-
 
1079
char *makeFileName(char *ret, char *fname, int len)
-
 
1080
{
-
 
1081
char *fdup, *pright, *cfg, *lcfg;
-
 
1082
 
-
 
1083
	if (ret == NULL || fname == NULL || !len || !strlen(fname))
-
 
1084
	   return NULL;
-
 
1085
 
-
 
1086
	if (!access(fname, R_OK | W_OK))
-
 
1087
	{
-
 
1088
	   memset(ret, 0, len);
-
 
1089
	   strncpy(ret, fname, len-1);
-
 
1090
	   return ret;
-
 
1091
	}
-
 
1092
 
-
 
1093
	fdup = strdup(fname);
-
 
1094
 
-
 
1095
	if (fdup == NULL)
-
 
1096
	{
-
 
1097
	   syslog(LOG_DAEMON, "Error allocating memory: %s", strerror(errno));
-
 
1098
	   return NULL;
-
 
1099
	}
-
 
1100
 
-
 
1101
	cfg = strdup(configs.HeizPath);
-
 
1102
	
-
 
1103
	if (cfg == NULL)
-
 
1104
	{
-
 
1105
	   syslog(LOG_DAEMON, "Error allocating memory: %s", strerror(errno));
-
 
1106
	   free(fdup);
-
 
1107
	   return NULL;
-
 
1108
	}
-
 
1109
 
-
 
1110
	pright = basename(fdup);
-
 
1111
	lcfg = dirname(cfg);
-
 
1112
	memset(ret, 0, len);
-
 
1113
	strncpy(ret, lcfg, len-1);
-
 
1114
	
-
 
1115
	if (strlen(ret) < (len - (strlen(pright) + 2)))
-
 
1116
	{
-
 
1117
	   strcat(ret, "/");
-
 
1118
	   strcat(ret, pright);
-
 
1119
	   free(fdup);
-
 
1120
	   free(cfg);
-
 
1121
	   return ret;
-
 
1122
	}
-
 
1123
	
-
 
1124
	free(fdup);
-
 
1125
	free(cfg);
-
 
1126
	return NULL;
-
 
1127
}
-
 
1128
 
925
int readHeizPlan(void)
1129
int readHeizPlan(void)
926
{
1130
{
-
 
1131
	return readHeizPlanName(configs.HeizPath);
-
 
1132
}
-
 
1133
 
-
 
1134
int readHeizPlanName(char* fname)
-
 
1135
{
927
int fd, i, wday;
1136
int fd, i, wday;
928
ulong tim;
1137
ulong tim;
929
char line[512];
1138
char line[512];
930
char *p, *xp;
1139
char *p, *xp;
931
char hv0[64], hv1[128];
1140
char hv0[64], hv1[128];
-
 
1141
char path[512];
932
float temperature;
1142
float temperature;
933
int counter = 0;
1143
int counter = 0;
934
HEIZINDEX *act, *prev;
1144
HEIZINDEX *act, *prev;
935
 
1145
 
936
	fd = -1;
1146
	fd = -1;
937
 
1147
 
-
 
1148
	if (makeFileName(&path[0], fname, sizeof(path)) == NULL)
-
 
1149
	{
-
 
1150
	   syslog(LOG_DAEMON,"No or invalid path %s", fname);
-
 
1151
	   return 0;
-
 
1152
	}
-
 
1153
 
938
	if (access(configs.HeizPath, R_OK))
1154
	if (access(path, R_OK))
939
	{
1155
	{
940
	   syslog(LOG_DAEMON,"Access to file %s denied: %s", configs.HeizPath, strerror(errno));
1156
	   syslog(LOG_DAEMON,"Access to file %s denied: %s", path, strerror(errno));
941
	   return 0;
1157
	   return 0;
942
	}
1158
	}
943
 
1159
 
944
	if ((fd = open(configs.HeizPath, O_RDONLY)) == -1)
1160
	if ((fd = open(path, O_RDONLY)) == -1)
945
	{
1161
	{
946
	   syslog(LOG_DAEMON,"Error opening file %s: %s", configs.HeizPath, strerror(errno));
1162
	   syslog(LOG_DAEMON,"Error opening file %s: %s", path, strerror(errno));
947
	   return 0;
1163
	   return 0;
948
	}
1164
	}
949
 
1165
 
950
	act = prev = NULL;
1166
	act = prev = NULL;
951
	freeMemory();
1167
	freeMemory();
Line 1046... Line 1262...
1046
 * write the (may be) altered plan.
1262
 * write the (may be) altered plan.
1047
 * This function allways writes whole plan.
1263
 * This function allways writes whole plan.
1048
 */
1264
 */
1049
int writeHeizPlan()
1265
int writeHeizPlan()
1050
{
1266
{
-
 
1267
	return writeHeizPlanName(configs.HeizPath);
-
 
1268
}
-
 
1269
 
-
 
1270
int writeHeizPlanName(char *fname)
-
 
1271
{
1051
int fd;
1272
int fd;
1052
char hv0[512];
1273
char hv0[512], path[512];
1053
HEIZINDEX *act;
1274
HEIZINDEX *act;
1054
 
1275
 
1055
	fd = -1;
1276
	fd = -1;
1056
 
1277
 
1057
	if (access(configs.HeizPath, R_OK))
1278
	if (makeFileName(&path[0], fname, sizeof(path)) == NULL)
-
 
1279
	{
-
 
1280
	   syslog(LOG_DAEMON,"No or invalid path %s", fname);
1058
	   return 0;
1281
	   return 0;
-
 
1282
	}
1059
 
1283
 
1060
	if ((fd = open(configs.HeizPath, O_RDWR | O_TRUNC)) == -1)
1284
	if ((fd = open(path, O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
-
 
1285
	{
-
 
1286
	   syslog(LOG_DAEMON, "Error opening/creating file %s: %s", path, strerror(errno));
1061
	   return 0;
1287
	   return 0;
-
 
1288
	}
1062
 
1289
 
1063
	act = HeizFirst;
1290
	act = HeizFirst;
1064
 
1291
 
1065
	while(act)
1292
	while(act)
1066
	{
1293
	{