SPAN class=postbodyLinux 에서 동적 착탈되는 파일시스템을 검출하는 예제입니다. br /개념적인 이해만 되면 USB drive 나 Floppy, CDROM 등을 검출하는 프로그램을 작성할수 있겠지요 br /br /현재 이 예제는 일단 USB drive 만 인식하도록 되어 있습니다. (정확히는 */sd* 장치를 검출하게 작성되었습니다. br /br /mzdetectfs.c br //SPAN
TABLE cellSpacing=1 cellPadding=3 width=90% align=center border=0
TBODY
TR
TDSPAN class=genmedB코드:/B/SPAN/TD/TR
TR
TD class=code/* br /nbsp; Copyright (C) Information Equipment co.,LTD br /nbsp; All rights reserved. br /nbsp; Code by JaeHyuk Cho lt;mailto:minzkn@infoeq.comgt; br /nbsp; CVSTAG=$Id$ br /*/ br /br /#include lt;sys/types.hgt; br /#include lt;sys/ioctl.hgt; br /#include lt;stdio.hgt; br /#include lt;stdlib.hgt; br /#include lt;string.hgt; br /#include lt;unistd.hgt; br /#include lt;fcntl.hgt; br /#include lt;getopt.hgt; br /#include lt;signal.hgt; br /#include lt;errno.hgt; br /br /#include partition.h br /br /#define __def_detectfs_debug__nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;(1) br /br /static volatile int g_mz_detectfs_break = 0; br /static volatile int g_mz_detectfs_verify = 0; br /br /static void mz_detectfs_signal(int s_signal) br /{ br /nbsp;switch(s_signal) br /nbsp;{ br /nbsp; case SIGINT: case SIGTERM: case SIGQUIT: g_mz_detectfs_break = 1; (void)signal(s_signal, mz_detectfs_signal); break; br /nbsp; case SIGSEGV: case SIGFPE: (void)raise(s_signal); exit(1); break; br /nbsp; default: if(g_mz_detectfs_verify != 0)(void)fprintf(stderr, detectfs: [ERROR] unknown signal ! (signal=%08XH)\n, s_signal); break; br /nbsp;} br /} br /br /static int (mz_detectfs_filter)(struct ts_mz_partition *s_partition) br /{ br /nbsp;if((s_partition-gt;name == ((char *)0)) || (s_partition-gt;name_size lt; ((size_t)3)))return(0); br /nbsp;if((s_partition-gt;name[0] != 's') || (s_partition-gt;name[1] != 'd'))return(0); br /nbsp;return(1); br /} br /br //* ------------------- */ br /static int (mz_detectfs_do_work)(int s_valid, struct ts_mz_partition *s_partition, const char *s_entry) br /{ br /nbsp;int s_result = 0; br /nbsp;if(s_valid != 0) br /nbsp;{ br /nbsp; if(g_mz_detectfs_verify != 0) br /nbsp; { br /nbsp; nbsp;unsigned char s_buffer[ 4 lt;lt; 10 ]; br /nbsp; nbsp;if(s_partition-gt;level[1] == 0u)(void)sprintf((char *)(amp;s_buffer[0]), %s/%u, s_entry, s_partition-gt;level[0]); br /nbsp; nbsp;else (void)sprintf((char *)(amp;s_buffer[0]), %s/%u/%u, s_entry, s_partition-gt;level[0], s_partition-gt;level[1]); br /nbsp; nbsp;(void)fprintf(stdout, entry[%s]\n, (char *)(amp;s_buffer[0])); br /nbsp; } br /nbsp;} br /nbsp;return(s_result); br /} br //* ------------------- */ br /br /int (main)(int s_argc, char **s_argv) br /{ br /nbsp;int s_result = 0, s_option, s_help = 0, s_interval = 1, s_check; br /nbsp;const char *s_entry = (char *)0; br /nbsp;struct ts_mz_partition *s_partition, *s_trace; br /nbsp;(void)signal(SIGINT, mz_detectfs_signal); (void)signal(SIGTERM, mz_detectfs_signal); (void)signal(SIGQUIT, mz_detectfs_signal); br /nbsp;(void)signal(SIGSEGV, mz_detectfs_signal); (void)signal(SIGFPE, mz_detectfs_signal); br /nbsp;while((s_option = getopt(s_argc, s_argv, hve:i:)) != (-1)) br /nbsp;{ br /nbsp; switch(s_option) br /nbsp; { br /nbsp; nbsp;case 'h': s_help = 1; br /nbsp; nbsp;case 'v': g_mz_detectfs_verify = 1; break; br /nbsp; nbsp;case 'e': s_entry = optarg; break; br /nbsp; nbsp;case 'i': if(sscanf(optarg, %d, amp;s_interval) != 1)s_interval = 1; break; br /nbsp; nbsp;default: break; br /nbsp; } br /nbsp;} br /nbsp;if(s_entry == ((char *)0))s_entry = /mnt/usb; br /nbsp;if(g_mz_detectfs_verify != 0)(void)fprintf(stdout, mzdetectfs v0.0.1 build 0 (%s %s) by lt;minzkn@infoeq.comgt;\n\n, __DATE__, __TIME__); br /nbsp;if(s_help != 0) br /nbsp;{ br /nbsp; (void)fprintf(stdout, br /nbsp; nbsp;Usage: %s [-h] [-e lt;mount entrygt;] [-i lt;intervalgt;] [-v]\n br /nbsp; nbsp;Options:\n br /nbsp; nbsp;\t-hnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;: display this help and exit\n br /nbsp; nbsp;\t-e lt;mount entrygt; : mount entry\n br /nbsp; nbsp;\t-i lt;intervalgt;nbsp; nbsp; : check interval (sec)\n br /nbsp; nbsp;\t-vnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;: be verbose\n, br /nbsp; nbsp;s_argv[0]); br /nbsp;} br /nbsp;else br /nbsp;{ br /nbsp; do br /nbsp; { br /nbsp; nbsp;s_trace = s_partition = mz_get_partition_list(/proc/partitions); br /nbsp; nbsp;if(s_partition != ((struct ts_mz_partition *)0)) br /nbsp; nbsp;{ br /nbsp; nbsp; while(s_trace != ((struct ts_mz_partition *)0)) br /nbsp; nbsp; { br /nbsp; nbsp; nbsp;s_check = mz_detectfs_filter(s_trace); br /nbsp; nbsp; nbsp; br /nbsp; nbsp; nbsp;(void)mz_detectfs_do_work(s_check, s_trace, s_entry); br /nbsp; nbsp; nbsp; br /nbsp; nbsp; nbsp;if(g_mz_detectfs_verify != 0) br /nbsp; nbsp; nbsp;{ br /nbsp; nbsp; nbsp; (void)fprintf(stdout, \tmajor=%3u, minor=%3u, blocks=%11llu, name_size=%2u, , br /nbsp; nbsp; nbsp; s_trace-gt;major, s_trace-gt;minor, s_trace-gt;blocks, (unsigned int)s_trace-gt;name_size); br /nbsp; nbsp; nbsp; if(s_check != 0)(void)fprintf(stdout, name=\\x1b[1;33m%s\x1b[0m\\n, s_trace-gt;name); br /nbsp; nbsp; nbsp; else (void)fprintf(stdout, name=\%s\\n, s_trace-gt;name); br /nbsp; nbsp; nbsp; if(s_trace-gt;next == ((struct ts_mz_partition *)0))(void)fputs(\n, stdout); br /nbsp; nbsp; nbsp;} br /nbsp; nbsp; nbsp;s_trace = s_trace-gt;next; br /nbsp; nbsp; } br /nbsp; nbsp; (void)mz_free_partition_list(s_partition); br /nbsp; nbsp;} br /nbsp; nbsp;if(s_interval gt; 0)(void)sleep(s_interval); br /nbsp; }while(g_mz_detectfs_break == 0); br /nbsp;} br /nbsp;if(g_mz_detectfs_verify != 0)(void)fputs(\nEnd of detectfs.\n, stdout); br /nbsp;return(s_result); br /} br /br //* vim: set expandtab: */ br //* End of source *//TD/TR/TBODY/TABLESPAN class=postbodybr /br /partition.c br //SPAN
TABLE cellSpacing=1 cellPadding=3 width=90% align=center border=0
TBODY
TR
TDSPAN class=genmedB코드:/B/SPAN/TD/TR
TR
TD class=code/* br /nbsp; Copyright (C) Information Equipment co.,LTD br /nbsp; All rights reserved. br /nbsp; Code by JaeHyuk Cho lt;mailto:minzkn@infoeq.comgt; br /nbsp; CVSTAG=$Id$ br /*/ br /br /#if !defined(__def_detectfs_source_partition_c__) br /#define __def_detectfs_source_partition_c__ br /br /#include lt;sys/types.hgt; br /#include lt;stdio.hgt; br /#include lt;stdlib.hgt; br /#include lt;string.hgt; br /#include lt;unistd.hgt; br /#include lt;fcntl.hgt; br /#include lt;errno.hgt; br /br /#include partition.h br /br /#define __def_detectfs_buffer_size__nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;(4 lt;lt; 10) br /br /size_t (mz_get_word_sep)(const char *s_sep, char **s_sep_string, void *s_data, size_t s_size); br /struct ts_mz_partition * (mz_new_partition)(void); br /struct ts_mz_partition * (mz_free_partition)(struct ts_mz_partition *s_partition); br /struct ts_mz_partition * (mz_parse_proc_partition_line)(const char *s_line); br /struct ts_mz_partition * (mz_get_partition_list)(const char *s_proc_path); br /struct ts_mz_partition * (mz_free_partition_list)(struct ts_mz_partition *s_partition); br /br /size_t (mz_get_word_sep)(const char *s_sep, char **s_sep_string, void *s_data, size_t s_size) br /{ br /nbsp;size_t s_result; br /nbsp;const char *s_left, *s_right, *s_sep_ptr; br /nbsp;char *s_string = *(s_sep_string); br /nbsp;while((*(s_string) == ' ') || (*(s_string) == '\t'))s_string++; br /nbsp;s_left = s_right = s_string; br /nbsp;while(*(s_string) != '\0') br /nbsp;{ br /nbsp; s_sep_ptr = s_sep; while((*(s_string) != *(s_sep_ptr)) amp;amp; (*(s_sep_ptr) != '\0'))s_sep_ptr++; br /nbsp; if(*(s_string) == *(s_sep_ptr))break; br /nbsp; if(*(s_string) != ' ')s_right = ++s_string; br /nbsp; else s_string++; br /nbsp;} br /nbsp;s_result = (size_t)(s_right - s_left); br /nbsp;(void)memcpy(s_data, s_left, (s_result gt; (s_size - ((size_t)1))) ? (s_size - ((size_t)1)) : s_result); br /nbsp;*(((char *)s_data) + s_result) = '\0'; br /nbsp;*(s_sep_string) = s_string; br /nbsp;return(s_result); br /} br /br /struct ts_mz_partition * (mz_new_partition)(void) br /{ br /nbsp;struct ts_mz_partition *s_partition = (struct ts_mz_partition *)malloc((size_t)sizeof(struct ts_mz_partition)); br /nbsp;if(s_partition != ((struct ts_mz_partition *)0))(void)memset((void *)s_partition, 0, (size_t)sizeof(struct ts_mz_partition)); br /nbsp;return(s_partition); br /} br /br /struct ts_mz_partition * (mz_free_partition)(struct ts_mz_partition *s_partition) br /{ br /nbsp;if(s_partition != ((struct ts_mz_partition *)0)) br /nbsp;{ br /nbsp; if(s_partition-gt;name != ((char *)0))free((void *)s_partition-gt;name); br /nbsp; free((void *)s_partition); br /nbsp;} br /nbsp;return((struct ts_mz_partition *)0); br /} br /br /struct ts_mz_partition * (mz_parse_proc_partition_line)(const char *s_line) br /{ br /nbsp;struct ts_mz_partition *s_new = (struct ts_mz_partition *)0; br /nbsp;const char *s_sep_string = s_line; br /nbsp;unsigned char s_buffer[ __def_detectfs_buffer_size__ ]; br /nbsp;int s_word_count = 0; br /nbsp;unsigned int s_major, s_minor; br /nbsp;unsigned long long s_blocks; br /nbsp;char *s_name; br /nbsp;size_t s_size; br /nbsp;do br /nbsp;{ br /nbsp; s_size = mz_get_word_sep( \t, (char **)(amp;s_sep_string), (void *)(amp;s_buffer[0]), (size_t)sizeof(s_buffer)); br /nbsp; if(*(s_sep_string) != '\0')s_sep_string++; br /nbsp; if(s_size lt;= ((size_t)0))break; br /nbsp; if(s_word_count == 0){ /* major */ if(sscanf((char *)(amp;s_buffer[0]), %u, amp;s_major) != 1)break; } br /nbsp; else if(s_word_count == 1){ /* minor */ if(sscanf((char *)(amp;s_buffer[0]), %u, amp;s_minor) != 1)break; } br /nbsp; else if(s_word_count == 2){ /* blocks */ if(sscanf((char *)(amp;s_buffer[0]), %llu, amp;s_blocks) != 1)break; } br /nbsp; else br /nbsp; { /* name */ br /nbsp; nbsp;s_name = (char *)malloc(strlen((char *)(amp;s_buffer[0])) + ((size_t)1)); br /nbsp; nbsp;if(s_name == ((char *)0))break; br /nbsp; nbsp;(void)strcpy(s_name, (char *)(amp;s_buffer[0])); br /nbsp; nbsp;s_new = mz_new_partition(); br /nbsp; nbsp;if(s_new == ((struct ts_mz_partition *)0))free((void *)s_name); br /nbsp; nbsp;else br /nbsp; nbsp;{ br /nbsp; nbsp; s_new-gt;major = s_major; br /nbsp; nbsp; s_new-gt;minor = s_minor; br /nbsp; nbsp; s_new-gt;level[0] = s_minor gt;gt; 4; br /nbsp; nbsp; s_new-gt;level[1] = s_minor amp; 0x0fu; br /nbsp; nbsp; s_new-gt;blocks = s_blocks; br /nbsp; nbsp; s_new-gt;name = s_name; br /nbsp; nbsp; s_new-gt;name_size = s_size; br /nbsp; nbsp;} br /nbsp; nbsp;break; br /nbsp; } br /nbsp; s_word_count++; br /nbsp;}while(*(s_sep_string) != '\0'); br /nbsp;return(s_new); br /} br /br /struct ts_mz_partition * (mz_get_partition_list)(const char *s_proc_path) br /{ br /nbsp;struct ts_mz_partition *s_partition = (struct ts_mz_partition *)0, *s_last = (struct ts_mz_partition *)0, *s_new; br /nbsp;int s_handle = open(s_proc_path, O_RDONLY); br /nbsp;unsigned char s_buffer[2][ __def_detectfs_buffer_size__ ]; br /nbsp;ssize_t s_read_bytes; br /nbsp;const char *s_sep_string = (char *)(amp;s_buffer[0][0]); br /nbsp;if(s_handle != (-1)) br /nbsp;{ br /nbsp; s_read_bytes = read(s_handle, (void *)(amp;s_buffer[0][0]), (size_t)(sizeof(s_buffer[0]) - 1)); br /nbsp; if(s_read_bytes gt; ((ssize_t)0)) br /nbsp; { br /nbsp; nbsp;s_buffer[0][s_read_bytes] = (unsigned char)'\0'; br /nbsp; nbsp;do br /nbsp; nbsp;{ br /nbsp; nbsp; (void)mz_get_word_sep(\n\r, (char **)(amp;s_sep_string), (void *)(amp;s_buffer[1][0]), (size_t)sizeof(s_buffer[1])); br /nbsp; nbsp; s_new = mz_parse_proc_partition_line((char *)(amp;s_buffer[1][0])); br /nbsp; nbsp; if(s_new != ((struct ts_mz_partition *)0)) br /nbsp; nbsp; { /* add */ br /nbsp; nbsp; nbsp;if(s_last != ((struct ts_mz_partition *)0))s_last-gt;next = s_new; br /nbsp; nbsp; nbsp;else s_partition = s_new; br /nbsp; nbsp; nbsp;s_last = s_new; br /nbsp; nbsp; } br /nbsp; nbsp; if(*(s_sep_string) != '\0')s_sep_string++; br /nbsp; nbsp;}while(*(s_sep_string) != '\0'); br /nbsp; } br /nbsp; (void)close(s_handle); br /nbsp;} br /nbsp;return(s_partition); br /} br /br /struct ts_mz_partition * (mz_free_partition_list)(struct ts_mz_partition *s_partition) br /{ br /nbsp;while(s_partition != ((struct ts_mz_partition *)0)){ struct ts_mz_partition *s_prev = s_partition; s_partition = s_partition-gt;next; (void)mz_free_partition(s_prev); } br /nbsp;return((struct ts_mz_partition *)0); br /} br /br /#endif br /br //* vim: set expandtab: */ br //* End of source *//TD/TR/TBODY/TABLESPAN class=postbodybr /br /partition.h br //SPAN
TABLE cellSpacing=1 cellPadding=3 width=90% align=center border=0
TBODY
TR
TDSPAN class=genmedB코드:/B/SPAN/TD/TR
TR
TD class=code/* br /nbsp; Copyright (C) Information Equipment co.,LTD br /nbsp; All rights reserved. br /nbsp; Code by JaeHyuk Cho lt;mailto:minzkn@infoeq.comgt; br /nbsp; CVSTAG=$Id$ br /*/ br /br /#if !defined(__def_detectfs_source_partition_h__) br /#define __def_detectfs_source_partition_h__ br /br /struct ts_mz_partition br /{ br /nbsp;struct ts_mz_partition *next; br /nbsp;unsigned int major, minor, level[2]; br /nbsp;unsigned long long blocks; br /nbsp;char *name; br /nbsp;size_t name_size; br /}; br /br /#if !defined(__def_detectfs_source_partition_c__) br /extern size_t (mz_get_word_sep)(const char *s_sep, char **s_sep_string, void *s_data, size_t s_size); br /extern struct ts_mz_partition * (mz_new_partition)(void); br /extern struct ts_mz_partition * (mz_free_partition)(struct ts_mz_partition *s_partition); br /extern struct ts_mz_partition * (mz_parse_proc_partition_line)(const char *s_line); br /extern struct ts_mz_partition * (mz_get_partition_list)(const char *s_proc_path); br /extern struct ts_mz_partition * (mz_free_partition_list)(struct ts_mz_partition *s_partition); br /#endif br /br /#endif br /br //* vim: set expandtab: */ br //* End of source *//TD/TR/TBODY/TABLESPAN class=postbodybr /br /Makefile br //SPAN
TABLE cellSpacing=1 cellPadding=3 width=90% align=center border=0
TBODY
TR
TDSPAN class=genmedB코드:/B/SPAN/TD/TR
TR
TD class=code# Copyright (C) Information Equipment co.,LTD br /# All rights reserved. br /# Code by JaeHyuk Cho lt;mailto:minzkn@infoeq.comgt; br /# CVSTAG=$Id$ br /br /CROSS_COMPILEnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; ?=# br /br /CCnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;:= $(CROSS_COMPILE)gcc# br /RMnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;:= rm -f# br /STRIPnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; := $(CROSS_COMPILE)strip# br /br /THIS_NAMEnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; := mzdetectfs# br /br /CFLAGSnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;:= -Os -pipe -ansi# br /CFLAGSnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;+= -Wall -Werror# br /CFLAGSnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;+= -fomit-frame-pointer# br /CFLAGSnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;+= -I.# br /br /LDFLAGSnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; := -s# br /br /TARGETnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;:= $(THIS_NAME)# br /OBJECTSnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; := $(THIS_NAME).o# br /OBJECTSnbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; += partition.o# br /br /.PHONY: all clean br /br /all: $(TARGET) ; $(STRIP) --remove-section=.comment --remove-section=.note $(^) br /clean: ; $(RM) *.o $(TARGET) br /$(TARGET): $(OBJECTS) ; $(CC) $(LDFLAGS) -o $(@) $(^) br /$(OBJECTS): Makefile br /%.o: %.c ; $(CC) $(CFLAGS) -c -o $(@) $(lt;) br /br /# End of Makefile/TD/TR/TBODY/TABLE
받은 트랙백이 없고,
댓글이 없습니다.
detectfs.tar.bz2
글
댓글을 달아 주세요
댓글 RSS 주소 : http://blog.minzkn.com/rss/comment/173댓글 ATOM 주소 : http://blog.minzkn.com/atom/comment/173