File: scriptlets.c

package info (click to toggle)
util-vserver 0.30.212-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,648 kB
  • ctags: 3,145
  • sloc: ansic: 19,781; sh: 13,258; xml: 1,618; makefile: 364; perl: 85
file content (171 lines) | stat: -rw-r--r-- 4,599 bytes parent folder | download | duplicates (3)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// $Id: scriptlets.c 1746 2004-10-19 21:11:10Z ensc $    --*- c -*--

// Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
//  
// 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; version 2 of the License.
//  
// 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.


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "vserver-start.h"

#include <pathconfig.h>
#include <lib_internal/command.h>
#include <lib_internal/util.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#include <dirent.h>

#define HAS_SUFFIX(STR, LEN, SUF) \
  (LEN>sizeof(SUF) && strcmp(STR+LEN-sizeof(SUF), SUF)==0)

static bool
visitFile(char const *fname, char const *vname, char const *style)
{
  struct stat		st;
  struct Command	cmd;

  if (stat(fname, &st)==-1 ||
      !S_ISREG(st.st_mode))
    return false;

  if ((st.st_mode & 0111)==0) {
    WRITE_MSG(2,
	      "!!!LEGACY ALERT!!!\n"
	      "The special handling of non-executable scriptlets which allows to\n"
	      "override environment variables is not supported anymore. This change\n"
	      "was needed as 'vserver ... start' is done by a native C program now.\n"
	      "If you need the old functionality please fill a bugreport so that\n"
	      "workarounds can be found/implemented.\n"
	      "The file triggering this message was\n"
	      "    '");
    WRITE_STR(2, fname);
    WRITE_MSG(2, "'\n");

    return false;
  }

  char const *par[] = { fname, style, vname, 0 };
  Command_setParams(&cmd, par);

  if (!Command_exec(&cmd, true) ||
      !Command_wait(&cmd, true)) {
    WRITE_MSG(2, "vserver-start: exec('");
    WRITE_STR(2, fname);
    WRITE_MSG(2, "'): ");
    WRITE_STR(2, strerror(cmd.err));
    WRITE_MSG(2, "; aborting...\n");

    exit(1);
  }

  if (cmd.rc!=0) {
    WRITE_MSG(2, "vserver-start: scriptlet '");
    WRITE_STR(2, fname);
    WRITE_MSG(2, "' failed; aborting...\n");
    
    exit (1);
  }

  Command_free(&cmd);

  return true;
}

static bool
visitDirentry(PathInfo const *basepath, char const *d_name,
	      char const *vname,
	      char const *style)
{
  size_t		l = strlen(d_name);
  char			path[basepath->l + l + 1];
  char *		ptr;

  if (isDotfile(d_name) ||
      HAS_SUFFIX(d_name, l, ".rpmnew") ||
      HAS_SUFFIX(d_name, l, ".rpmsave") ||
      HAS_SUFFIX(d_name, l, ".rpmorig") ||
      HAS_SUFFIX(d_name, l, ".cfsaved"))
    return false;

  ptr  = Xmemcpy(path, basepath->d, basepath->l);
  ptr  = Xmemcpy(ptr,  d_name,      l);
  *ptr = '\0';
  
  return visitFile(path, vname, style);
}

static bool
visitPath(PathInfo const *basepath,
	  char const *vname,
	  PathInfo const *style)
{
  char		tmp[basepath->l + style->l + sizeof(".d/")];
  PathInfo	path = { .d = tmp };
  char *	ptr;
  DIR *		dir;
  bool		did_something = false;

  ptr  = Xmemcpy(tmp, basepath->d, basepath->l);
  ptr  = Xmemcpy(ptr, style->d,    style->l);
  *ptr = '\0';
  path.l = ptr-tmp;

  did_something = visitFile(path.d, vname, style->d) || did_something;

  ptr = Xmemcpy(ptr, ".d/", sizeof(".d/"));
  path.l = ptr-tmp;
  
  dir = opendir(tmp);
  while (dir) {
    struct dirent *ent	=  readdir(dir);
    if (ent==0) break;

    did_something = visitDirentry(&path, ent->d_name, vname, style->d) || did_something;
  }
  if (dir!=0) closedir(dir);

  return did_something;
}

void
execScriptlets(PathInfo const *cfgdir, char const *vname, char const *style)
{
  char		path_buf[MAX(cfgdir->l, sizeof(CONFDIR "/.defaults")) +
			 sizeof("/scripts/")];
  PathInfo	basepath = { .d = path_buf };
  PathInfo	styledir = {
    .d = style,
    .l = strlen(style)
  };
  char *	ptr;
  bool		doit = true;

  ptr        = Xmemcpy(path_buf, cfgdir->d, cfgdir->l);
  ptr        = Xmemcpy(ptr,      "/scripts/", sizeof("/scripts/"));
  basepath.l = ptr-path_buf-1;
  doit       = !visitPath(&basepath, vname, &styledir);

  if (doit) {
    ptr = Xmemcpy(path_buf, CONFDIR "/.defaults/scripts/",
		  sizeof(CONFDIR "/.defaults/scripts/"));
    basepath.l = ptr-path_buf-1;
    doit = !visitPath(&basepath, vname, &styledir);
  }
}