summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/ntwdog/ntcron.c
blob: 1cb31300b5582a03f057c1c29b7ff984e28597e1 (plain)
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
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
//                                                                          //
//  Name: NTCRON                                                            //
//	 Platforms: WIN32                                                       //
//  Description: unix cron functionality in a separate thread               //
//  Notes:                                                                  //
//  The following assumptions are made:                                     //
//  - gszServerRoot is set to c:\netscape\server                            //
//  - ns-cron.conf and cron.conf are available                              //
//  Todo:                                                                   //
//  - handle time format variations of hh:mm                                //
//  - keep track of children                                                //
//  ......................................................................  //
//  Revision History:                                                       //
//  03-26-96  Initial Version, Andy Hakim (ahakim@netscape.com)             //
//  07-10-96  Modified for Directory Server, pkennedy@netscape.com			//
//--------------------------------------------------------------------------//
#include <windows.h>
#include "ntwatchdog.h"
#include "ntslapdmessages.h" // event log msgs constants //
#include "cron_conf.h"

static cron_conf_list *cclist   = NULL;


//--------------------------------------------------------------------------//
//                                                                          //
//--------------------------------------------------------------------------//
BOOL CRON_CheckDay(LPSYSTEMTIME lpstNow, char *szDays)
{
	BOOL bReturn = FALSE;
	char szToday[16];
	if(GetDateFormat((LCID)NULL, 0, lpstNow, "ddd", szToday, sizeof(szToday)) != 0)
	{
		strlwr(szDays);
		strlwr(szToday);
		if(strstr(szDays, szToday) != NULL)
			bReturn = TRUE;
	}
	return(bReturn);
}



//--------------------------------------------------------------------------//
//                                                                          //
//--------------------------------------------------------------------------//
BOOL CRON_CheckTime(LPSYSTEMTIME lpstNow, char *szTime)
{
	BOOL bReturn = FALSE;
	char szCurrentTime[16];
	char szStartTime[16];

	strncpy(szStartTime, szTime, sizeof(szStartTime)-1);

	if(szTime[1] == ':')
		wsprintf(szStartTime, "0%s", szTime);

	if(GetTimeFormat((LCID)LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, lpstNow, "hh:mm", szCurrentTime, sizeof(szCurrentTime)) != 0)
	{
		if(strcmp(szCurrentTime, szStartTime) == 0)
			bReturn = TRUE;
	}
	return(bReturn);
}



//--------------------------------------------------------------------------//
//                                                                          //
//--------------------------------------------------------------------------//
BOOL CRON_StartJob(PROCESS_INFORMATION *pi, cron_conf_obj *cco)
{
	BOOL bReturn = FALSE;
	STARTUPINFO sui;

   sui.cb               = sizeof(STARTUPINFO);
   sui.lpReserved       = 0;
   sui.lpDesktop        = NULL;
   sui.lpTitle          = NULL;
   sui.dwX              = 0;
   sui.dwY              = 0;
   sui.dwXSize          = 0;
   sui.dwYSize          = 0;
   sui.dwXCountChars    = 0;
   sui.dwYCountChars    = 0;
   sui.dwFillAttribute  = 0;
   sui.dwFlags          = STARTF_USESHOWWINDOW;
   sui.wShowWindow      = SW_SHOWMINIMIZED;
   sui.cbReserved2      = 0;
   sui.lpReserved2      = 0;
   sui.hStdInput 			= 0;
   sui.hStdOutput 		= 0;
   sui.hStdError 			= 0;
	
	bReturn = CreateProcess(NULL, cco->command, NULL, NULL,
                   TRUE, 0, NULL, cco->dir, &sui, pi );
	if(!bReturn)
		WD_SysLog(EVENTLOG_ERROR_TYPE, MSG_CRON_STARTFAILED, cco->name);

	return(bReturn);
}


//--------------------------------------------------------------------------//
//                                                                          //
//--------------------------------------------------------------------------//
BOOL CRON_CheckConfFile()
{
	BOOL bReturn = FALSE;
	PROCESS_INFORMATION pi;
	SYSTEMTIME stNow;

	GetLocalTime(&stNow); // note: this provides time adjusted for local time zone
	
	if(cron_conf_read())
		cclist = cron_conf_get_list();

	while((cclist) && (cclist->obj))
	{
		cron_conf_obj *cco = cclist->obj;
	 	if((cco->days) && (cco->start_time) && (cco->command))
		{
			if(CRON_CheckDay(&stNow, cco->days) && CRON_CheckTime(&stNow, cco->start_time))
			{
				bReturn = CRON_StartJob(&pi, cco);
            CLOSEHANDLE(pi.hProcess);
            CLOSEHANDLE(pi.hThread);
			}
		}
		cclist = cclist->next;
	}
	cron_conf_free();
	return bReturn;
}



//--------------------------------------------------------------------------//
//                                                                          //
//--------------------------------------------------------------------------//
LPTHREAD_START_ROUTINE CRON_ThreadProc(HANDLE hevWatchDogExit)
{
	BOOL bExit = FALSE;
	while(!bExit)
	{
		CRON_CheckConfFile();
		if(WaitForSingleObject(hevWatchDogExit, 1000*DEFAULT_CRON_TIME) != WAIT_TIMEOUT)
			bExit = TRUE;
	}
	return 0;
}