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
|
/*
* binfmt_misc C Interpreter
* Copyright (C) 2005 Junichi Uekawa
*
* 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
*
* Interepreter for binfmt_misc C compilation.
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/wait.h>
#include "binfmtc.h"
#define BINFMTC_MAGIC_LEN strlen(binfmtc_magic)
#define BINFMTC_DEBUG (getenv("BINFMTC_DEBUG"))
char* compile_source(const char *sourcename)
{
char* origdir = getcwd(NULL, 0);
char* path = strdup (sourcename);
char* path_delimiter;
char* tempfilename = NULL;
char* s = NULL;
char* gcccommandline = NULL;
size_t size;
FILE* f;
int i;
asprintf(&tempfilename, "%s/binfmtcXXXXXX",
getenv("BINFMTCTMPDIR")?:
getenv("TMPDIR")?:
getenv("TEMPDIR")?:
"/tmp"
);
if (NULL==(path_delimiter=strrchr(path, '/')))
{
fprintf(stderr,
"binfmtc: Could not determine the directory name of source %s\n",
sourcename);
return NULL;
}
*path_delimiter=0;/* obtain the dirname */
close(mkstemp(tempfilename)); /* get temporary filename */
if (!(f=fopen(sourcename,"rt")))
{
fprintf(stderr,
"binfmtc: Could not open file %s for read\n",
sourcename);
unlink(tempfilename);
return NULL;
}
if (!getline (&s, &size, f))
{
fprintf(stderr,
"binfmtc: Could not read file %s\n",
sourcename);
unlink(tempfilename);
return NULL;
}
/* compare with magic */
if (size < BINFMTC_MAGIC_LEN ||
(memcmp(binfmtc_magic, s, BINFMTC_MAGIC_LEN)))
{
fprintf(stderr,
"binfmtc: %s magic invalid \n",
sourcename);
unlink(tempfilename);
return NULL;
}
fclose(f);
/* make the newlines removed */
for(i=strlen(s)-1; i; --i)
{
if (!isspace (s[i]))
break;
else
s[i]=0;
}
asprintf (&gcccommandline,
"%s -o %s %s %s %s \"%s\" %s %s",
compiler_name(),
tempfilename,
gcc_x,
s+BINFMTC_MAGIC_LEN,
default_options(),
basename(sourcename),
s+BINFMTC_MAGIC_LEN,
default_options());
if (BINFMTC_DEBUG)
fprintf(stderr, "binfmtc: Execute command-line: %s\n", gcccommandline);
if (chdir(path)) /* go to the source's dir */
{
fprintf(stderr,
"binfmtc: Cannot chdir to directory where source is: %s \n",
path);
unlink(tempfilename);
return NULL;
}
if (system(gcccommandline))
{
fprintf(stderr,
"binfmtc: Compilation failed for %s, see above messages for details\n",
sourcename);
unlink(tempfilename);
return NULL;
}
if (chdir(origdir))
{
fprintf(stderr,
"binfmtc: Cannot chdir to original working directory: %s \n",
path);
unlink(tempfilename);
return NULL;
}
free(origdir);
free(path);
free(s);
free(gcccommandline);
return tempfilename;
}
/**
Function to execute the compiled program.
The reason the compiled program is execed as the parent program
is to allow use of same PID for the compiled program, so that
it is possible to use "strace" "kill -HUP" and other conventional
methods of debugging.
*/
int exec_prog(const char * filename, int argc, char**argv)
{
int pid;
int parent_pid;
switch(pid=fork())
{
case -1:
/* fork failed */
fprintf(stderr,
"binfmtc: Failed to fork \n");
return EXIT_FAILURE;
break;
case 0:
/* when I am the child process, unlink the file after the parent
process has exited.
*/
parent_pid=getppid();
if (daemon(0,0) < 0)
{
exit (-1);
}
while ((parent_pid!=-1)
&& (kill(parent_pid,0) >= 0))
{
sleep(1);
}
unlink(filename);
exit(0);
default:
/* I am the parent process, exec into the built program */
waitpid(pid,NULL,0);
execvp(filename, argv);
fprintf(stderr,
"binfmtc: failed execvp of %s \n", filename);
break;
}
return EXIT_FAILURE;
}
int main(int argc, char**argv)
{
char* filename=NULL;
int exit_code = EXIT_FAILURE;
if (argc < 2)
{
/* Wrong number of command-line args; maybe someone tried to exec me directly. */
fprintf(stderr,
"binfmtc: Wrong number of command-line options, use binfmt_misc\n%s\n",
language_type);
}
else if ((filename=compile_source(argv[1])))
exit_code=exec_prog(filename, argc-1, &(argv[1]));
return exit_code;
}
|