📄 autousb.c
字号:
/* autousb.c: mount usb disk automaticly This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation;*/#define _GNU_SOURCE#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>#include <sys/stat.h>#include <string.h>#include <signal.h>#include <sys/param.h>#include <stdio.h>#include "autousb.h"extern int get_par(char *dev, struct par_info *p);static void handler(int sig, siginfo_t *si, void *data);static int write_fstab(char *dev, char *mount_point, char *systype);static int _mount_dev(char *mount_point);static int chk_usb_attached(char *file_name);const char * sysname(unsigned char type);int mount_dev(int num);struct systypes { unsigned char type; char *name;} sys_types[] = { {0, "auto"}, {6, "vfat"}, {0x53, "ext2"}};int main(){ fprintf(stderr,"Leo USB-Auto File Manage Tool\n"); struct sigaction act; int fd; act.sa_sigaction = handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_SIGINFO; sigaction(SIGRTMIN, &act, NULL); while(1){ DBG("start pause...\n"); pause(); } return 0;}//handler temp use beginstatic void handler(int sig, siginfo_t *si, void *data){ char buf[] = "ls /proc/scsi/"; FILE *ls; char *line = NULL; int len; ls = popen(buf, "r"); if (!ls){ perror("popen"); exit(-1); } while(getline(&line, &len, ls) >= 0){ if (strstr(line, "usb-storage-")) chk_usb_attached(line);//if we use ls /proc/scis,look at usb storage //we will call function: chk_usb_attached } pclose(ls);}//test ,is there is usb disk plug instatic int chk_usb_attached(char *file_name){//the file_name is the result of "ls /proc/scsi/" char path[32], path_file[32]; int fd; char line[1024]; int i; int j; for (i = 0; file_name[i] != '\n'; i++){ path[i] = file_name[i]; } path[i] = '\0'; DBG("path:%s\n", path); i = atoi(&path[12]); sprintf(path_file, "/proc/scsi/usb-storage-%d/%d", i, i); fd = open(path_file, O_RDONLY); if (fd < 0 ){ DBG("fopen error :%d\n", __LINE__); exit(-1); } for (j=0; j<1024; j++) line[j] = '0'; if (read(fd, line, 1024) > 0){ //DBG("%s \n", line); if (strstr(line, "Attached: Yes")){ DBG("Attached: Yes\n"); mount_dev(i); } } close(fd); return 0; }static int write_fstab(char *dev, char *mount_point, char *systype){ FILE *fp_fstab; char *line = NULL; int len = 0; DBG("starting %12s", __FUNCTION__); if (fp_fstab = fopen("/etc/fstab", "a+")){ while(getline(&line, &len, fp_fstab) >=0){ if (strstr(line, dev)) goto out; } } else exit(-1); mkdir(mount_point, 666); fprintf(fp_fstab, "%-23s %-23s %-7s %-15s %d %d\n", dev, mount_point, systype, "noauto,kudzu,owner", 0, 0); out: fclose(fp_fstab); DBG(" ok\n"); return 0;}int mount_dev(int num){ char dev[12]; FILE *fp_mnt; char *line = NULL; char mount_point[128]; int len; int i = 0, j; struct par_info par[64]; //memset(dev,0, 12); for (j=0; j<12; j++) dev[j] = '0'; memset(mount_point, 0, 128); sprintf(dev, "/dev/sd%c", 'a'+num); DBG("starting %12s num: %d\n", __FUNCTION__, num); if (get_par(dev, par) < 0) exit(-1); if (par[0].type == 0){ goto nopartitions; } while (par[i].type != 0){ memcpy(dev, par[i++].dev, 12); nopartitions: sprintf(mount_point, "/mnt%s",&(dev[4])); if (fp_mnt = fopen("/proc/mounts", "r")){ while (getline(&line, &len, fp_mnt) >= 0) { if (strstr(line, dev)) goto mounted; } } else exit(-1); DBG("dev: %s, mount_point: %s\n", dev, mount_point); write_fstab(dev, mount_point, sysname(par[i].type)); _mount_dev(mount_point); mounted: fclose(fp_mnt); } free(line); return 0;} static int _mount_dev(char *mount_point){ char *argv[] = {"/bin/mount", NULL, NULL}; pid_t pid; //char tmp[64]; //int code = 950; int status; //FILE *fp_cfg; DBG("starting %12s", __FUNCTION__); if (mount_point == NULL) return 1; argv[1] = mount_point; if (!(pid = fork())){ execv(argv[0], argv); _exit(1); } waitpid(pid, &status, 0); if (!WIFEXITED(status) || (WEXITSTATUS(status))){ DBG(" failed\n"); return (-1); } DBG(" ok\n"); return 0;}const char * sysname(unsigned char type){ struct systypes *s; for (s = sys_types; s - sys_types < SIZE(sys_types); s++) if (s->type == type) return s->name; return "auto";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -