|
|
575cdb |
/*******************************************************************/
|
|
|
575cdb |
/* slibtool: a skinny libtool implementation, written in C */
|
|
|
78d96a |
/* Copyright (C) 2016--2017 Z. Gilboa */
|
|
|
575cdb |
/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
|
|
|
575cdb |
/*******************************************************************/
|
|
|
575cdb |
|
|
|
575cdb |
#include <stdio.h>
|
|
|
575cdb |
#include <limits.h>
|
|
|
575cdb |
#include <unistd.h>
|
|
|
575cdb |
#include <fcntl.h>
|
|
|
575cdb |
#include <string.h>
|
|
|
575cdb |
#include <stdlib.h>
|
|
|
575cdb |
#include <stdbool.h>
|
|
|
575cdb |
#include <sys/wait.h>
|
|
|
575cdb |
|
|
|
575cdb |
#include <slibtool/slibtool.h>
|
|
|
575cdb |
#include "slibtool_spawn_impl.h"
|
|
|
575cdb |
|
|
|
d986a0 |
static void slbt_dump_machine_child(
|
|
|
575cdb |
char * program,
|
|
|
575cdb |
int fd[2])
|
|
|
575cdb |
{
|
|
|
575cdb |
char * compiler;
|
|
|
575cdb |
char * argv[3];
|
|
|
575cdb |
|
|
|
575cdb |
if ((compiler = strchr(program,'/')))
|
|
|
575cdb |
compiler++;
|
|
|
575cdb |
else
|
|
|
575cdb |
compiler = program;
|
|
|
575cdb |
|
|
|
575cdb |
argv[0] = compiler;
|
|
|
575cdb |
argv[1] = "-dumpmachine";
|
|
|
575cdb |
argv[2] = 0;
|
|
|
575cdb |
|
|
|
575cdb |
close(fd[0]);
|
|
|
575cdb |
|
|
|
f4ed8e |
if ((fd[0] = open("/dev/null",O_RDONLY)) >= 0)
|
|
|
f4ed8e |
if (dup2(fd[0],0) == 0)
|
|
|
f4ed8e |
if (dup2(fd[1],1) == 1)
|
|
|
f4ed8e |
execvp(program,argv);
|
|
|
575cdb |
|
|
|
ec6d7a |
_exit(EXIT_FAILURE);
|
|
|
575cdb |
}
|
|
|
575cdb |
|
|
|
575cdb |
int slbt_dump_machine(
|
|
|
575cdb |
const char * compiler,
|
|
|
575cdb |
char * machine,
|
|
|
575cdb |
size_t bufsize)
|
|
|
575cdb |
{
|
|
|
575cdb |
pid_t pid;
|
|
|
575cdb |
pid_t rpid;
|
|
|
575cdb |
int code;
|
|
|
575cdb |
int fd[2];
|
|
|
575cdb |
FILE * fmachine;
|
|
|
575cdb |
char * newline;
|
|
|
c67e64 |
char * mark;
|
|
|
575cdb |
char check[2];
|
|
|
575cdb |
char program[PATH_MAX];
|
|
|
575cdb |
|
|
|
575cdb |
if (!machine || !bufsize)
|
|
|
575cdb |
return -1;
|
|
|
575cdb |
|
|
|
575cdb |
if ((size_t)snprintf(program,sizeof(program),"%s",
|
|
|
575cdb |
compiler) >= sizeof(program))
|
|
|
575cdb |
return -1;
|
|
|
575cdb |
|
|
|
575cdb |
if (pipe(fd))
|
|
|
575cdb |
return -1;
|
|
|
575cdb |
|
|
|
575cdb |
if ((pid = fork()) < 0) {
|
|
|
575cdb |
close(fd[0]);
|
|
|
575cdb |
close(fd[1]);
|
|
|
575cdb |
return -1;
|
|
|
575cdb |
}
|
|
|
575cdb |
|
|
|
575cdb |
if (pid == 0)
|
|
|
d986a0 |
slbt_dump_machine_child(
|
|
|
575cdb |
program,
|
|
|
575cdb |
fd);
|
|
|
575cdb |
|
|
|
575cdb |
rpid = waitpid(
|
|
|
575cdb |
pid,
|
|
|
575cdb |
&code,
|
|
|
575cdb |
0);
|
|
|
575cdb |
|
|
|
575cdb |
if ((rpid != pid) || code) {
|
|
|
575cdb |
close(fd[0]);
|
|
|
575cdb |
close(fd[1]);
|
|
|
575cdb |
return -1;
|
|
|
575cdb |
}
|
|
|
575cdb |
|
|
|
575cdb |
if ((fmachine = fdopen(fd[0],"r"))) {
|
|
|
575cdb |
close(fd[1]);
|
|
|
575cdb |
newline = 0;
|
|
|
575cdb |
|
|
|
575cdb |
if (fgets(machine,bufsize,fmachine) == machine)
|
|
|
575cdb |
if (!fgets(check,sizeof(check),fmachine))
|
|
|
575cdb |
if (feof(fmachine))
|
|
|
575cdb |
if ((newline = strrchr(machine,'\n')))
|
|
|
575cdb |
*newline = 0;
|
|
|
575cdb |
|
|
|
575cdb |
fclose(fmachine);
|
|
|
575cdb |
} else {
|
|
|
575cdb |
newline = 0;
|
|
|
575cdb |
close(fd[0]);
|
|
|
575cdb |
close(fd[1]);
|
|
|
575cdb |
}
|
|
|
575cdb |
|
|
|
c67e64 |
/* support the portbld <--> unknown synonym */
|
|
|
c67e64 |
if (newline)
|
|
|
c67e64 |
if ((mark = strstr(machine,"-portbld-")))
|
|
|
c67e64 |
memcpy(mark,"-unknown",8);
|
|
|
c67e64 |
|
|
|
575cdb |
return newline ? 0 : -1;
|
|
|
575cdb |
}
|