/*
* auto-apt.so
* on demand package installation tool
* Copyright (c) 2000 Fumitoshi UKAI <[email protected]>
* GPL
*
*/
static char auto_apt_rcsid[] __attribute__ ((unused)) = "$Id: auto-apt.c,v 1.28 2000/12/04 14:27:47 ukai Exp $";
#define LARGEFILE_SOURCE
#define LARGEFILE64_SOURCE
#define __USE_LARGEFILE64 1
#define __USE_FILE_OFFSET64 1
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#define PKGCDB_AUTOAPT 1
#include "pkgcdb/debug.h"
#include "pkgcdb/pkgcdb2.h"
#include "pkgcdb/mempool.c"
#include "pkgcdb/strtab.c"
#include "pkgcdb/pkgtab.c"
#include "pkgcdb/pathnode.c"
#include "pkgcdb/pkgcdb2.c"
#define APT_HOOK_EXEC 0
#define APT_HOOK_OPEN 1
#define APT_HOOK_ACCESS 2
#define APT_HOOK_STAT 3
#define NUM_APT_HOOK 4
static int apt_hook[NUM_APT_HOOK];
static char *pkgcdb_file = PKGCDB_FILE;
static PathNodeTree pkgcdb_tree = NULL;
static char *filedb_file = NULL;
static PathNodeTree filedb_tree = NULL;
#ifdef __alpha__
#define LIBCPATH "/lib/libc.so.6.1"
#else
#define LIBCPATH "/lib/libc.so.6"
#endif
typedef int (*funcptr)();
static struct realfunctab {
char *name;
funcptr fptr;
} rftab[] = {
{"execve", NULL}, /* execve(2) */
/* XXX: execl(3), execle(3) */
/* execlp(3)->execvp(3) */
/* execvp(3)->execv(3) */
{"execv", NULL},
{"open", NULL},
{"open64", NULL},
#if 1
{"__libc_open", NULL},
{"__libc_open64", NULL},
#endif
{"access", NULL},
{"euidaccess", NULL},
{"__xstat", NULL},
{"__xstat64", NULL},
{"__lxstat", NULL},
{"__lxstat64", NULL},
{NULL, NULL}
};
static struct path_node *filename2package(PathNodeTree pnt,
const char *filename,
char **detect);
static int command_line_name(char *buf, size_t siz);
static funcptr
load_library_symbol(char *name)
{
void *handle;
const char *error;
struct realfunctab *ft;
char *libcpath = NULL;
for (ft = rftab; ft->name; ft++) {
if (strcmp(name, ft->name) == 0) {
if (ft->fptr != NULL) {
return ft->fptr;
}
break;
}
}
if (ft->name == NULL) {
DPRINT(("func:%s not found\n", name));
return NULL;
}
if ((libcpath = getenv("LIBC_PATH")) == NULL)
libcpath = LIBCPATH;
handle = dlopen (libcpath, RTLD_LAZY);
if (!handle) {
DPRINT((dlerror()));
return NULL;
}
ft->fptr = dlsym(handle, ft->name);
if ((error = dlerror()) != NULL) {
DPRINT(("dysym(%s)=%s\n", ft->name, error));
ft->fptr = NULL;
}
dlclose(handle);
return ft->fptr;
}
static int
open_internal(const char *filename, int flag, int mode)
{
funcptr __open = load_library_symbol("__libc_open64");
if (__open == NULL) __open = load_library_symbol("__libc_open");
if (__open == NULL) __open = load_library_symbol("open64");
if (__open == NULL) __open = load_library_symbol("open");
if (__open == NULL)
return -1;
return __open(filename, flag, mode);
}
static char *
auto_apt_conf_var(char *name, char *def)
{
char *p = getenv(name);
if (p == NULL)
return def;
if (*p == '\0')
return def;
return p;
}
static int
auto_apt_conf_switch(char *name)
{
char *p = auto_apt_conf_var(name, NULL);
if (p == NULL)
return 0;
if (strcasecmp(p, "no") == 0 || strcasecmp(p, "off") == 0)
return 0;
return 1;
}
#ifdef USE_DETECT
static char *detectdb_file = NULL;
static char *detectdb_lockfile = NULL;
/* detectdb will access from multiple process, need lock */
static int
detectdb_lock()
{
struct flock fl;
if (detectdb_lock != NULL) {
int fd = open_internal(detectdb_lockfile,
O_RDWR|O_CREAT|O_TRUNC, 0660);
if (fd == -1) {
abort();
return -1;
}
again:
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 1;
if (fcntl(fd, F_SETLK, &fl) == -1) {
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EACCES)
goto again;
close(fd);
abort();
return -1;
}
return fd;
}
abort();
return -1;
}
static void
detectdb_unlock(int fd)
{
struct flock fl;
if (detectdb_lock != NULL) {
if (fd >= 0) {
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 1;
if (fcntl(fd, F_SETLK, &fl) == -1) {
/* cannot happen? */
}
close(fd);
}
unlink(detectdb_lockfile);
}
}
static int
detect_package(const char *filename, char *func)
{
int e = 0;
struct path_node *pn;
char *file = NULL;
char *p = NULL;
char pkg[PATH_MAX];
int lockfd;
if (detectdb_file == NULL)
return -1;
if (filedb_tree == NULL)
return -1;
DPRINT(("detect: %s\n", filename));
/* ignore db files */
if (strcmp(filename, pkgcdb_file) == 0) {
return 0;
} else if (strcmp(filename, detectdb_file) == 0) {
return 0;
} else if (detectdb_lockfile &&
strcmp(filename, detectdb_lockfile) == 0) {
return 0;
} else if (filedb_file &&
strcmp(filename, filedb_file) == 0) {
return 0;
}
DPRINT(("check by filedb: %s\n", filename));
pn = filename2package(filedb_tree, filename, &file);
if (pn == NULL) {
DPRINT(("no package file, ignore\n"));
return -1;
}
p = pathnode_packagename(filedb_tree, pn);
if (p == NULL || *p == '!' || *p == '@') {
DPRINT(("dummy package, ignore\n"));
return -1;
}
strncpy(pkg, p, PATH_MAX-1);
DPRINT(("put it to detect file: %s => %s %s\n", filename, file, pkg));
if (file == NULL)
file = (char *)filename;
/* XXX: need lock */
lockfd = detectdb_lock();
if (lockfd < 0)
goto done;
{
char cmd[PATH_MAX];
int fd = open_internal(detectdb_file,
O_WRONLY|O_APPEND|O_CREAT, 0644);
if (fd < 0) {
abort();
goto done;
}
/* <file> <package> <func>(<filename>) <cmd> */
write(fd, file, strlen(file));
write(fd, "\t", 1);
write(fd, pkg, strlen(pkg));
write(fd, "\t", 1);
write(fd, func, strlen(func));
write(fd, "(", 1);
write(fd, filename, strlen(filename));
write(fd, ")\t", 2);
command_line_name(cmd, PATH_MAX-1);
write(fd, cmd, strlen(cmd));
write(fd, "\n", 1);
close(fd);
}
done:
/* XXX: need unlock */
detectdb_unlock(lockfd);
return e;
}
#else
#define detect_package(filename,func)
#endif
/* _init() ? */
static void
auto_apt_setup()
{
static int inited = 0;
if (!inited) {
char *p;
int i;
inited = 1;
mempool_init();
pkgtab_init();
for (i = 0; i < NUM_APT_HOOK; i++) {
apt_hook[i] = 0;
}
p = auto_apt_conf_var("AUTO_APT_HOOK", "none");
if (p != NULL) {
apt_hook[APT_HOOK_EXEC] = (strstr(p, "exec") != NULL);
apt_hook[APT_HOOK_OPEN] = (strstr(p, "open") != NULL);
apt_hook[APT_HOOK_ACCESS] = (strstr(p, "access") != NULL);
apt_hook[APT_HOOK_STAT] = (strstr(p, "stat") != NULL);
if (strcmp(p, "all") == 0) {
for (i = 0; i < NUM_APT_HOOK; i++) {
apt_hook[i] = 1;
}
}
if (strstr(p, "none") != NULL) {
for (i = 0; i < NUM_APT_HOOK; i++) {
apt_hook[i] = 0;
}
}
}
if (auto_apt_conf_switch("AUTO_APT_DEBUG")) {
debug = 1;
}
if (auto_apt_conf_switch("AUTO_APT_QUIET")) {
quiet = 1;
}
if (auto_apt_conf_switch("AUTO_APT_VERBOSE")) {
verbose = 1;
}
p = auto_apt_conf_var("AUTO_APT_DB", PKGCDB_FILE);
if (p != NULL && *p == '/') {
pkgcdb_file = strdup(p);
pkgcdb_tree = pkgcdb_load(pkgcdb_file, 0, 0);
if (pkgcdb_tree == NULL) {
VMSG(("auto-apt %s load failed, auto-apt off\n", pkgcdb_file));
unsetenv("LD_PRELOAD");
free(pkgcdb_file);
pkgcdb_file = NULL;
/* should exit() here ?*/
}
}
#ifdef USE_DETECT
p = auto_apt_conf_var("AUTO_APT_DETECT", NULL);
if (p != NULL) {
detectdb_file = strdup(p);
DPRINT(("detectdb_file=%s\n", detectdb_file));
detectdb_lockfile = malloc(strlen(detectdb_file) + 5);
if (detectdb_lockfile != NULL) {
sprintf(d
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
auto-apt_0.3.21+nmu1.tar.gz (37个子文件)
auto-apt-0.3.21+nmu1
debian
control 790B
postrm 974B
dirs 61B
changelog 13KB
substvars 52B
compat 2B
docs 17B
examples 30B
copyright 451B
rules 1KB
auto-apt.c 29KB
pkgcdb
pathnode.c 11KB
pkgtab.c 2KB
pkgcdb2.h 2KB
mempool.h 1KB
pathnode.h 2KB
strtab.h 809B
pkgtab.h 759B
pkgcdb2.c 12KB
mempool.c 6KB
Makefile 295B
debug.h 1KB
strtab.c 5KB
examples
strace2builddepends.pl 2KB
auto-apt-get-menu.sh 2KB
auto-apt-installer.pl 8KB
auto-apt.1 5KB
BUGS 2KB
TODO 891B
commands.list 119B
auto-apt.es.1 5KB
README 2KB
auto-apt-pkgcdb.c 8KB
Makefile 1KB
paths.list 2KB
auto-apt.sh 15KB
auto-apt.fr.1 7KB
共 37 条
- 1
资源评论
- tveht2022-10-31还不错,就是弄起来有点麻烦。
- mushroomying02122012-10-09资源很好、、但是我的虚拟机不知道为什么就是拒绝安装、、希望可以介绍的再详细点好了
linuxdrake
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功