Merge branch 'v1.2.1' into 'v1.2.1'

【新增】时区修改时间监测报时

See merge request kysdk/kysdk-system!24
This commit is contained in:
陈志开 2022-07-28 03:23:59 +00:00
commit 052f6c4fec
1 changed files with 78 additions and 6 deletions

View File

@ -13,6 +13,8 @@
#include <semaphore.h>
#include <sys/timerfd.h>
#include <errno.h>
// 20020721新增时区变化监听
#include <sys/inotify.h>
pthread_mutex_t lock;
u_int8_t g_Flag; // 控制启用常驻定时器还是临时定时器
@ -39,12 +41,16 @@ static void *printClock(void *ptr)
time_t current;
time(&current);
now = localtime(&current);
// struct timeval tx;
// struct timezone tz;
// gettimeofday(&tx,&tz);
// zone = tz.tz_minuteswest/60;
// printf("时差:%d\n",zone);
// 如果时间发生改变发送TimeChangeSignal信号
if (g_TimeChanged == 1)
{
char *buf = calloc(1, 128);
sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", now->tm_year + 1900, now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", now->tm_year + 1900, now->tm_mon+1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
printf("%s\n", buf);
msg = dbus_message_new_signal("/com/kylin/kysdk/Timer",
@ -83,7 +89,7 @@ static void *printClock(void *ptr)
else
{
char *buf = calloc(1, 128);
sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", now->tm_year + 1900, now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", now->tm_year + 1900, now->tm_mon+1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
// printf("%s\n", buf);
msg = dbus_message_new_signal("/com/kylin/kysdk/Timer",
@ -148,7 +154,7 @@ void *startBroadcastSystemTimePerMin(void *tmp)
int monitorSystemTimeChange()
{
#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
// printf("monitorSystemTimeChange\n");
struct itimerspec its = {.it_value.tv_sec = TIME_T_MAX};
int fd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
if (fd < 0)
@ -190,6 +196,57 @@ void *actionTimeChanged(void *ptr)
return NULL;
}
// 20020721新增时区变化监听
int monitorSystemTimeZoneChange(){
char buf[BUFSIZ];
int fd = inotify_init();
buf[sizeof(buf) - 1] = 0;
struct inotify_event *event;
if (fd < 0)
{
return -1;
}
int ftimezone = inotify_add_watch(fd,"/etc/timezone",IN_DELETE_SELF);
if (ftimezone < 0)
{
close(fd);
return -1;
}
int ret = read(fd, buf, sizeof(buf) - 1);
close(fd);
event = (struct inotify_event *)&buf[0];
if (ret)
{
fprintf(stdout, "%s --- %s\n", event->name, "IN_DELETE_SELF");
return 1;
}
return 0;
}
// 20020721新增时区变化监听
void *actionTimeZoneChanged(void *ptr)
{
DBusConnection *conn = ptr;
while (!g_Quit)
{
if (monitorSystemTimeZoneChange() == 1)
{
printf("System Time Changed.\n");
if (g_Flag)
{
g_TimeChanged = 1;
g_Flag = 0;
printClock(conn);
}
}
}
return NULL;
}
const char *version = "0.1";
GMainLoop *mainloop;
@ -233,7 +290,15 @@ const char *server_introspection_xml =
" </interface>\n"
" <interface name='org.freedesktop.DBus.Properties'>\n"
" <method name=timerfd"
" <method name='Get'>\n"
" <arg name='interface' type='s' direction='in' />\n"
" <arg name='property' type='s' direction='in' />\n"
" <arg name='value' type='s' direction='out' />\n"
" </method>\n"
" <method name='GetAll'>\n"
" <arg name='interface' type='s' direction='in'/>\n"
" <arg name='properties' type='a{sv}' direction='out'/>\n"
" </method>\n"
" </interface>\n"
" <interface name='com.kylin.kysdk.TimeInterface'>\n"
@ -442,13 +507,20 @@ int main(void)
sem_init(&g_Wait, 0, 1);
pthread_mutex_init(&lock, NULL);
pthread_attr_t attr;
pthread_t tid;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, actionTimeChanged, conn);
// 20020721新增时区变化监听
pthread_attr_t timezone_attr;
pthread_t timezone_id;
pthread_attr_init(&timezone_attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&timezone_id, &timezone_attr, actionTimeZoneChanged, conn);
pthread_mutex_init(&lock, NULL);
/* connect to the daemon bus */