summaryrefslogtreecommitdiff
path: root/debian/patches/crash0001-trans-crash-xxx-core-file-templates.patch
blob: 64301bf21fbf7ba19a707c922bb282ea08cab29d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
From ae4dd9fd78060db8ed1db337b1c0302f32c4829b Mon Sep 17 00:00:00 2001
From: Justus Winter <justus@gnupg.org>
Date: Fri, 3 Jun 2016 00:52:06 +0200
Subject: [PATCH hurd] trans/crash: xxx core file templates

---
 trans/crash.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 157 insertions(+), 2 deletions(-)

diff --git a/trans/crash.c b/trans/crash.c
index 5db9974..3a4af12 100644
--- a/trans/crash.c
+++ b/trans/crash.c
@@ -70,7 +70,106 @@ enum crash_action
 #define CRASH_ORPHANS_DEFAULT	crash_corefile
 
 static enum crash_action crash_how, crash_orphans_how;
+static char *corefile_template;
 
+
+
+/* Template parsing.  */
+static int
+template_valid (const char *template, const char **errp)
+{
+  int valid = 0;
+  const char *t;
+  int specifier = 0;
+
+  for (t = template; *t; t++)
+    {
+      if (specifier)
+	switch (*t)
+	  {
+	  case '%':
+	  case 'p':
+	  case 's':
+	  case 't':
+	    specifier = 0;
+	    break;
+	  default:
+	    goto out;
+	  }
+      else if (*t == '%')
+	specifier = 1;
+    }
+
+  valid = ! specifier;
+
+ out:
+  *errp = valid? NULL: t;
+  return valid;
+}
+
+static char *
+template_make_file_name (const char *template,
+			 task_t task,
+			 int signo)
+{
+  const char *t;
+  char *file_name = NULL;
+  size_t file_name_len = 0;
+  FILE *stream;
+  int specifier = 0;
+
+  if (! template_valid (template, &t))
+    {
+      errno = EINVAL;
+      return NULL;
+    }
+
+  stream = open_memstream (&file_name, &file_name_len);
+  if (stream == NULL)
+    return NULL;
+
+  for (t = template; *t; t++)
+    {
+      if (specifier)
+	{
+	  switch (*t)
+	    {
+	    case '%':
+	      fprintf (stream, "%%");
+	      break;
+
+	    case 'p':
+	      fprintf (stream, "%d", task2pid (task));
+	      break;
+
+	    case 's':
+	      fprintf (stream, "%d", signo);
+	      break;
+
+	    case 't':
+	      fprintf (stream, "%d", time (NULL));
+	      break;
+
+	    default:
+	      assert (!"reached!");
+	    }
+	  specifier = 0;
+	}
+      else if (*t == '%')
+	specifier = 1;
+      else
+	fprintf (stream, "%c", *t);
+    }
+
+  assert (! specifier);
+
+  fprintf (stream, "%c", 0);
+  fclose (stream);
+
+  return file_name;
+}
+
+
 
 /* This is defined in ../exec/elfcore.c, or we could have
    different implementations for other formats.  */
@@ -235,10 +334,34 @@ S_crash_dump_task (mach_port_t port,
       err = task_suspend (task);
       if (!err)
 	{
-	  err = dump_core (task, core_file,
+	  file_t sink = core_file;
+	  if (corefile_template)
+	    {
+	      char *file_name;
+
+	      file_name = template_make_file_name (corefile_template,
+						   task, signo);
+	      if (file_name == NULL)
+		error (0, errno, "template_make_file_name");
+	      else
+		{
+		  sink = file_name_lookup (file_name, O_WRONLY|O_CREAT, 400);
+		  if (! MACH_PORT_VALID (sink))
+		    {
+		      error (0, errno, "%s", file_name);
+		      sink = core_file;
+		    }
+		  free (file_name);
+		}
+	    }
+
+	  err = dump_core (task, sink,
 			   (off_t) -1,	/* XXX should get core limit in RPC */
 			   signo, sigcode, sigerror);
 	  task_resume (task);
+
+	  if (sink != core_file)
+	    mach_port_deallocate (mach_task_self (), sink);
 	}
       break;
 
@@ -445,13 +568,25 @@ static const struct argp_option options[] =
   {"kill",	'k', 0,		0, "Kill the process", 2},
   {"core-file", 'c', 0,		0, "Dump a core file", 2},
   {"dump-core",   0, 0,		OPTION_ALIAS },
+  {"core-file-name", 'C', "TEMPLATE", 0,
+   "Specify core file name (see below)", 2},
   {0}
 };
 static const char doc[] =
 "Server to handle crashing tasks and dump core files or equivalent.\v"
 "The ACTION values can be `suspend', `kill', or `core-file'.\n\n"
 "If `--orphan-action' is not specified, the `--action' value is used for "
-"orphans.  The default is `--action=suspend --orphan-action=core-file'.";
+"orphans.  The default is `--action=suspend --orphan-action=core-file'.\n"
+"\n"
+"The core file is either written to the file provided by the "
+"crashing process, or if a TEMPLATE value is given, to the file "
+"with the name constructed by expanding TEMPLATE value.  "
+"TEMPLATE may contain % specifiers:\n"
+"\n"
+"\t%%  just %\n"
+"\t%p  the process' PID\n"
+"\t%s  the signal number that caused the dump\n"
+"\t%t  time of crash in seconds since the EPOCH\n";
 
 static error_t
 parse_opt (int opt, char *arg, struct argp_state *state)
@@ -489,6 +624,14 @@ parse_opt (int opt, char *arg, struct argp_state *state)
     case 's': crash_how = crash_suspend;	break;
     case 'k': crash_how = crash_kill;		break;
     case 'c': crash_how = crash_corefile;	break;
+    case 'C':
+      {
+	char *errp;
+	if (! template_valid (arg, &errp))
+	  error (1, 0, "Invalid template: ...'%s'", errp);
+      }
+      corefile_template = arg;
+      break;
 
     case ARGP_KEY_SUCCESS:
       if (crash_orphans_how == crash_unspecified)
@@ -531,6 +674,18 @@ trivfs_append_args (struct trivfs_control *fsys,
       err = argz_add (argz, argz_len, opt);
     }
 
+  if (!err && corefile_template)
+    {
+      char *template;
+      if (asprintf (&template, "--core-file-name=%s", corefile_template) < 0)
+	err = errno;
+      else
+	{
+	  err = argz_add (argz, argz_len, template);
+	  free (template);
+	}
+    }
+
   return err;
 }
 
-- 
2.1.4