Mercurial > hg > nnchat
comparison main.c @ 556:9b72d0060b85
Create configuration and log files with reduced permissions (rw for user
only) under UNIX.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 12 Nov 2012 20:08:07 +0200 |
parents | 66d18a82d6b9 |
children | 02244c553741 |
comparison
equal
deleted
inserted
replaced
555:66d18a82d6b9 | 556:9b72d0060b85 |
---|---|
995 | 995 |
996 int nncmd_save_config(nn_conn_t *conn, char *buf) | 996 int nncmd_save_config(nn_conn_t *conn, char *buf) |
997 { | 997 { |
998 (void) conn; | 998 (void) conn; |
999 (void) buf; | 999 (void) buf; |
1000 | 1000 FILE *cfgfile = NULL; |
1001 FILE *cfgfile = fopen(setConfigFile, "w"); | 1001 #ifndef __WIN32 |
1002 if (cfgfile == NULL) | 1002 int cfgfd = -1; |
1003 { | 1003 #endif |
1004 printMsgQ(currWin, "Could not create configuration to file '%s': %s\n", | 1004 |
1005 setConfigFile, strerror(errno)); | 1005 #ifdef __WIN32 |
1006 return 0; | 1006 if ((cfgfile = fopen(setConfigFile, "w")) == NULL) |
1007 #else | |
1008 if ((cfgfd = open(setConfigFile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || | |
1009 (cfgfile = fdopen(cfgfd, "w")) == NULL) | |
1010 #endif | |
1011 { | |
1012 printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n", | |
1013 setConfigFile, errno, strerror(errno)); | |
1014 goto error; | |
1007 } | 1015 } |
1008 | 1016 |
1009 printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n", | 1017 printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n", |
1010 setConfigFile, | 1018 setConfigFile, |
1011 th_cfg_write(cfgfile, setConfigFile, cfg)); | 1019 th_cfg_write(cfgfile, setConfigFile, cfg)); |
1012 | 1020 |
1013 fclose(cfgfile); | 1021 error: |
1022 if (cfgfile != NULL) | |
1023 fclose(cfgfile); | |
1024 #ifndef __WIN32 | |
1025 else | |
1026 if (cfgfd >= 0) | |
1027 close(cfgfd); | |
1028 #endif | |
1014 return 0; | 1029 return 0; |
1015 } | 1030 } |
1016 | 1031 |
1017 | 1032 |
1018 enum | 1033 enum |
1531 | 1546 |
1532 | 1547 |
1533 BOOL nn_log_open(nn_window_t *win) | 1548 BOOL nn_log_open(nn_window_t *win) |
1534 { | 1549 { |
1535 char *path = NULL; | 1550 char *path = NULL; |
1551 #ifndef __WIN32 | |
1552 int logFd = -1; | |
1553 #endif | |
1536 | 1554 |
1537 if (!optLogEnable) | 1555 if (!optLogEnable) |
1538 return FALSE; | 1556 return FALSE; |
1539 | 1557 |
1540 if (optLogPath != NULL) | 1558 if (optLogPath != NULL) |
1586 | 1604 |
1587 // Try to open the file for appending | 1605 // Try to open the file for appending |
1588 if (win->logFilename == NULL) | 1606 if (win->logFilename == NULL) |
1589 goto error; | 1607 goto error; |
1590 | 1608 |
1609 #ifdef __WIN32 | |
1591 if ((win->logFile = fopen(win->logFilename, "a")) == NULL) | 1610 if ((win->logFile = fopen(win->logFilename, "a")) == NULL) |
1592 { | 1611 #else |
1593 errorMsg("Could not open logfile '%s' for appending!\n", win->logFilename); | 1612 if ((logFd = open(win->logFilename, O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || |
1613 (win->logFile = fdopen(logFd, "a")) == NULL) | |
1614 #endif | |
1615 { | |
1616 errorMsg("Could not open logfile '%s' for appending, %d: %s\n", | |
1617 win->logFilename, errno, strerror(errno)); | |
1594 goto error; | 1618 goto error; |
1595 } | 1619 } |
1596 | 1620 |
1597 printMsg(win, "Logging to '%s'.\n", win->logFilename); | 1621 printMsg(win, "Logging to '%s'.\n", win->logFilename); |
1598 | 1622 |
1601 | 1625 |
1602 error: | 1626 error: |
1603 th_free(path); | 1627 th_free(path); |
1604 th_free(win->logFilename); | 1628 th_free(win->logFilename); |
1605 win->logFilename = NULL; | 1629 win->logFilename = NULL; |
1630 if (win->logFile != NULL) | |
1631 fclose(win->logFile); | |
1632 #ifndef __WIN32 | |
1633 else | |
1634 if (logFd >= 0) | |
1635 close(logFd); | |
1636 #endif | |
1606 return FALSE; | 1637 return FALSE; |
1607 } | 1638 } |
1608 | 1639 |
1609 | 1640 |
1610 void nn_log_close(nn_window_t *win) | 1641 void nn_log_close(nn_window_t *win) |