File: debug_crontab.c

package info (click to toggle)
wzdftpd 0.8.1-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 5,440 kB
  • ctags: 3,559
  • sloc: ansic: 40,580; sh: 9,306; lex: 2,082; makefile: 528; sql: 206; perl: 18
file content (157 lines) | stat: -rw-r--r-- 4,402 bytes parent folder | download | duplicates (4)
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
157
/* vi:ai:et:ts=8 sw=2
 */
/*
 * wzdftpd - a modular and cool ftp server
 * Copyright (C) 2002-2004  Pierre Chifflier
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * As a special exemption, Pierre Chifflier
 * and other respective copyright holders give permission to link this program
 * with OpenSSL, and distribute the resulting executable, without including
 * the source code for OpenSSL in the source distribution.
 */

#ifndef WIN32
#include <unistd.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <libwzd-core/wzd_structs.h>
#include <libwzd-core/wzd_libmain.h>
#include <libwzd-core/wzd_log.h>
#include <libwzd-core/wzd_messages.h>

#include <libwzd-core/wzd_crontab.h>

#include "debug_crontab.h"

static int _cron_find_and_execute(const char * jobname, wzd_cronjob_t * crontab);

int do_site_listcrontab(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
  int ret;
  char buffer[4096];
  wzd_cronjob_t * cronjob;
  time_t now;

  send_message_raw("200-\r\n",context);
  send_message_raw(" Name                              Min  Hour Day  Mon  DayOfWeek Next\r\n",context);

  WZD_MUTEX_LOCK(SET_MUTEX_CRONTAB);
  cronjob = getlib_mainConfig()->crontab;

  time(&now);

  while (cronjob != NULL) {

    snprintf(buffer,sizeof(buffer)," %-33s %-4s %-4s %-4s %-4s %-9s %-5ld\n",cronjob->hook->external_command,
        cronjob->minutes, cronjob->hours, cronjob->day_of_month, cronjob->month,
        cronjob->day_of_week, (long)(cronjob->next_run - now));
    ret = send_message_raw(buffer,context);

    cronjob = cronjob->next_cronjob;
  }

  ret = send_message_raw("200 command ok\r\n",context);

  WZD_MUTEX_UNLOCK(SET_MUTEX_CRONTAB);
  return 0;
}

int do_site_cronjob(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
  int ret, status;
  char buffer[4096];
  wzd_string_t * commandname, * jobname;

  commandname = str_tok(param," \t\r\n");
  if (!commandname) {
    ret = send_message_with_args(501,context,"site cronjob exec jobname");
    return -1;
  }

  ret = -1;
  if (strcasecmp(str_tochar(commandname),"exec")==0) {
    jobname = str_read_token(param);

    if (jobname) {
      send_message_raw("200-\r\n",context);

      status = _cron_find_and_execute(str_tochar(jobname),getlib_mainConfig()->crontab);

      snprintf(buffer,sizeof(buffer)-1," cron job: %s\n",str_tochar(jobname));
      ret = send_message_raw(buffer,context);

      if (status == 0)
        ret = send_message_raw("200 command ok\r\n",context);
      else if (status == -1)
        ret = send_message_raw("200 command failed (no cron job with this name)\r\n",context);
      else
        ret = send_message_raw("200 command ok (with errors)\r\n",context);
      ret = 0;
    } else {
      ret = send_message_with_args(501,context,"site cronjob exec jobname");
      ret = -1;
    }
  } else {
    ret = send_message_with_args(501,context,"site cronjob exec jobname");
    ret = -1;
  }

  str_deallocate(jobname);
  str_deallocate(commandname);

  return ret;
}



static int _cron_find_and_execute(const char * jobname, wzd_cronjob_t * crontab)
{
  int status = -1;
  wzd_cronjob_t *job;
  time_t now;

  job = malloc(sizeof(wzd_cronjob_t));

  WZD_MUTEX_LOCK(SET_MUTEX_CRONTAB);
  while (crontab != NULL) {
    if (crontab->hook && crontab->hook->external_command &&
        strcmp(crontab->hook->external_command,jobname)==0) {
      memcpy(job, crontab, sizeof(wzd_cronjob_t));
      time(&now);
      job->next_run = now;
      job->next_cronjob = NULL;
      status = 0;
      break;
    }
    crontab = crontab->next_cronjob;
  }

  WZD_MUTEX_UNLOCK(SET_MUTEX_CRONTAB);

  if (status == 0) {
    cronjob_run(&job);
  }

  free(job);

  return status;
}