~ubuntu-branches/ubuntu/trusty/dhcpcd/trusty-security

« back to all changes in this revision

Viewing changes to src/signals.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Kelley
  • Date: 2005-09-30 02:21:51 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050930022151-vq3xlcazj0bdpyf4
Tags: 1:2.0.0-2
Clear out /etc/dhcpc/resolv.conf and /var/lib/dhcpc/* 
during purge. (closes: #330515)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * dhcpcd - DHCP client daemon -
 
3
 * Copyright (C) 1996 - 1997 Yoichi Hariguchi <yoichi@fore.com>
 
4
 * Copyright (C) January, 1998 Sergei Viznyuk <sv@phystech.com>
 
5
 * Copyright (C) 2005 Roy Marples <uberlord@gentoo.org>
 
6
 * Copyright (C) 2005 Simon Kelley <simon@thekelleys.org.uk> 
 
7
 * 
 
8
 * dhcpcd is an RFC2131 and RFC1541 compliant DHCP client daemon.
 
9
 *
 
10
 * This is free software; you can redistribute it and/or modify it
 
11
 * under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
18
 * See the GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
23
 */
 
24
 
 
25
#include <sys/types.h>
 
26
#include <sys/wait.h>
 
27
#include <stdio.h>
 
28
#include <unistd.h>
 
29
#include <syslog.h>
 
30
#include <signal.h>
 
31
#include <setjmp.h>
 
32
#include <stdlib.h>
 
33
#include "pathnames.h"
 
34
#include "client.h"
 
35
 
 
36
extern char             *ProgramName;
 
37
extern char             *IfNameExt;
 
38
extern char             *ConfigDir;
 
39
extern int              DebugFlag;
 
40
extern int              Persistent;
 
41
extern jmp_buf          env;
 
42
extern void             *(*currState)();
 
43
/*****************************************************************************/
 
44
void killPid(sig)
 
45
int sig;
 
46
{
 
47
  FILE *fp;
 
48
  pid_t pid;
 
49
  char pidfile[64];
 
50
  snprintf(pidfile,sizeof(pidfile),PID_FILE_PATH,IfNameExt);
 
51
  fp=fopen(pidfile,"r");
 
52
  if ( fp == NULL ) goto ntrn;
 
53
  fscanf(fp,"%u",&pid);
 
54
  fclose(fp);
 
55
  if ( kill(pid,sig) )
 
56
    {
 
57
      unlink(pidfile);
 
58
ntrn: if ( sig == SIGALRM ) return;
 
59
      fprintf(stderr,"****  %s: not running\n",ProgramName);
 
60
    }
 
61
  exit(0);
 
62
}
 
63
/*****************************************************************************/
 
64
void writePidFile(pid_t pid)
 
65
{
 
66
  FILE *fp;
 
67
  char pidfile[64];
 
68
  snprintf(pidfile,sizeof(pidfile),PID_FILE_PATH,IfNameExt);
 
69
  fp=fopen(pidfile,"w");
 
70
  if ( fp == NULL )
 
71
    {
 
72
      syslog(LOG_ERR,"writePidFile: fopen: %m\n");
 
73
      exit(1);
 
74
    }
 
75
  fprintf(fp,"%u\n",pid);
 
76
  fclose (fp);
 
77
}
 
78
/*****************************************************************************/
 
79
void deletePidFile()
 
80
{
 
81
  char pidfile[64];
 
82
  snprintf(pidfile,sizeof(pidfile),PID_FILE_PATH,IfNameExt);
 
83
  unlink(pidfile);
 
84
}
 
85
/*****************************************************************************/
 
86
void sigHandler(sig)
 
87
int sig;
 
88
{
 
89
  if( sig == SIGCHLD )
 
90
    {
 
91
      waitpid(-1,NULL,WNOHANG);
 
92
      return;
 
93
    }
 
94
  if ( sig == SIGALRM )
 
95
    {
 
96
      if ( currState == &dhcpBound )
 
97
        siglongjmp(env,1); /* this timeout is T1 */
 
98
      else
 
99
        {
 
100
          if ( currState == &dhcpRenew )
 
101
            siglongjmp(env,2); /* this timeout is T2 */
 
102
          else
 
103
            {
 
104
              if ( currState == &dhcpRebind )
 
105
                siglongjmp(env,3);  /* this timeout is dhcpIpLeaseTime */
 
106
              else
 
107
                {
 
108
                  if ( currState == &dhcpReboot )
 
109
                    siglongjmp(env,4);  /* failed to acquire the same IP address */
 
110
                  else
 
111
                    syslog(LOG_ERR,"timed out waiting for a valid DHCP server response\n");
 
112
                }
 
113
            }
 
114
        }
 
115
    }
 
116
  else
 
117
    {
 
118
      if ( sig == SIGHUP ) 
 
119
        {
 
120
          dhcpRelease();
 
121
          /* allow time for final packets to be transmitted before shutting down     */
 
122
          /* otherwise 2.0 drops unsent packets. fixme: find a better way than sleep */
 
123
          sleep(1);
 
124
        }
 
125
        syslog(LOG_ERR,"terminating on signal %d\n",sig);
 
126
    }
 
127
  if (!Persistent || sig != SIGTERM)
 
128
    dhcpStop();
 
129
  deletePidFile();
 
130
  exit(sig);
 
131
}
 
132
/*****************************************************************************/
 
133
void signalSetup()
 
134
{
 
135
  int i;
 
136
  struct sigaction action;
 
137
  sigaction(SIGHUP,NULL,&action);
 
138
  action.sa_handler= &sigHandler;
 
139
  action.sa_flags = 0;
 
140
  for (i=1;i<16;i++) sigaction(i,&action,NULL);
 
141
  sigaction(SIGCHLD,&action,NULL);
 
142
}