Update main function

This commit is contained in:
topjohnwu
2017-04-15 18:10:54 +08:00
parent 6ad993704c
commit f0751007f3
5 changed files with 88 additions and 13 deletions

View File

@@ -135,3 +135,18 @@ void ps_filter_proc_name(const char *pattern, void (*func)(int)) {
ps_filter_pattern = ((pattern == NULL) ? "" : pattern);
ps(proc_name_filter);
}
int create_links(const char *bin, const char *path) {
char self[PATH_MAX], linkpath[PATH_MAX];
if (bin == NULL) {
xreadlink("/proc/self/exe", self, PATH_MAX);
bin = self;
}
int ret = 0;
for (int i = 0; applet[i]; ++i) {
snprintf(linkpath, PATH_MAX, "%s/%s", path, applet[i]);
unlink(linkpath);
ret |= symlink(bin, linkpath);
}
return ret;
}

View File

@@ -46,6 +46,8 @@ int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
int xsocketpair(int domain, int type, int protocol, int sv[2]);
int xstat(const char *pathname, struct stat *buf);
int xdup2(int oldfd, int newfd);
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz);
int xsymlink(const char *target, const char *linkpath);
// misc.c
@@ -58,5 +60,6 @@ int isNum(const char *s);
ssize_t fdreadline(int fd, char *buf, size_t size);
void ps(void (*func)(int));
void ps_filter_proc_name(const char *filter, void (*func)(int));
int create_links(const char *bin, const char *path);
#endif

View File

@@ -226,4 +226,22 @@ int xdup2(int oldfd, int newfd) {
return ret;
}
ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz) {
ssize_t ret = readlink(pathname, buf, bufsiz);
if (ret == -1) {
PLOGE("readlink %s", pathname);
} else {
buf[ret] = '\0';
}
return ret;
}
int xsymlink(const char *target, const char *linkpath) {
int ret = symlink(target, linkpath);
if (ret == -1) {
PLOGE("symlink %s->%s", target, linkpath);
}
return ret;
}