@@ -61,6 +61,7 @@ struct tlog_log {
61
61
char suffix [TLOG_LOG_NAME_LEN ];
62
62
char pending_logfile [PATH_MAX ];
63
63
int rename_pending ;
64
+ int fail ;
64
65
int logsize ;
65
66
int logcount ;
66
67
int block ;
@@ -1072,7 +1073,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
1072
1073
int len ;
1073
1074
int unused __attribute__ ((unused ));
1074
1075
1075
- if (bufflen <= 0 ) {
1076
+ if (bufflen <= 0 || log -> fail ) {
1076
1077
return 0 ;
1077
1078
}
1078
1079
@@ -1599,6 +1600,7 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
1599
1600
log -> filesize = 0 ;
1600
1601
log -> zip_pid = -1 ;
1601
1602
log -> is_exit = 0 ;
1603
+ log -> fail = 0 ;
1602
1604
log -> waiters = 0 ;
1603
1605
log -> block = ((flag & TLOG_NONBLOCK ) == 0 ) ? 1 : 0 ;
1604
1606
log -> nocompress = ((flag & TLOG_NOCOMPRESS ) == 0 ) ? 0 : 1 ;
@@ -1660,6 +1662,48 @@ void tlog_rename_logfile(struct tlog_log *log, const char *logfile)
1660
1662
log -> rename_pending = 1 ;
1661
1663
}
1662
1664
1665
+ static void tlog_fork_prepare () {
1666
+ if (tlog .root == NULL ) {
1667
+ return ;
1668
+ }
1669
+
1670
+ pthread_mutex_lock (& tlog .lock );
1671
+ }
1672
+
1673
+ static void tlog_fork_parent () {
1674
+ if (tlog .root == NULL ) {
1675
+ return ;
1676
+ }
1677
+
1678
+ pthread_mutex_unlock (& tlog .lock );
1679
+ }
1680
+
1681
+ static void tlog_fork_child () {
1682
+ pthread_attr_t attr ;
1683
+ tlog_log * next ;
1684
+ if (tlog .root == NULL ) {
1685
+ return ;
1686
+ }
1687
+
1688
+ pthread_attr_init (& attr );
1689
+ int ret = pthread_create (& tlog .tid , & attr , _tlog_work , NULL );
1690
+ if (ret != 0 ) {
1691
+ fprintf (stderr , "create tlog work thread failed, %s\n" , strerror (errno ));
1692
+ goto errout ;
1693
+ }
1694
+
1695
+ goto out ;
1696
+ errout :
1697
+ next = tlog .log ;
1698
+ while (next ) {
1699
+ next -> fail = 1 ;
1700
+ next = next -> next ;
1701
+ }
1702
+ out :
1703
+ pthread_mutex_unlock (& tlog .lock );
1704
+
1705
+ }
1706
+
1663
1707
int tlog_init (const char * logfile , int maxlogsize , int maxlogcount , int buffsize , unsigned int flag )
1664
1708
{
1665
1709
pthread_attr_t attr ;
@@ -1700,6 +1744,9 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
1700
1744
}
1701
1745
1702
1746
tlog .root = log ;
1747
+ if (flag & TLOG_SUPPORT_FORK ) {
1748
+ pthread_atfork (& tlog_fork_prepare , & tlog_fork_parent , & tlog_fork_child );
1749
+ }
1703
1750
return 0 ;
1704
1751
errout :
1705
1752
if (tlog .tid > 0 ) {
0 commit comments