【转】 struct mntent

 
【转】 struct mntent
2010-11-02 11:08
转载自 fembed
最终编辑 feng_zhao06831

在 struct mntent 中的成员与 /etc/fstab 文件中的条目是直接对应的。它的内容如下:

struct mntent {
char *mnt_fsname; /* 挂载的文件系统的名字 */
char *mnt_dir; /* 挂载点 */
char *mnt_type; /* 文件系统类型:ufs、nfs 等 */
char *mnt_opts; /* 选项,以逗号为分隔符 */
int mnt_freq; /* Dump 的频率(以天为单位) */
int mnt_passno; /* fsck检查的次序 */
};

FILE *setmntent(const char *filename, const char *type);
struct mntent *getmntent(FILE *filep);
int addmntent(FILE *filep, const struct mntent *mnt);
int endmntent(FILE *filep);
char *hasmntopt(const struct mntent *mnt, const char *opt);

setmntent() 是打开包含挂载点项目的文件, 其中的 filename 参数是要打开的文件名, type 参数就像 fopen() 的第二个参数, 代表只读、只写, 或读写皆可的存取模式 。返回FILE*。

getmntent() 则是循序读取整个档案,传回指向 static struct mntent 结构的指针,结构中会填入适当的值。

addmntent() 可以在已开启档案的末端加上资讯,它原本是给 mount 使用的。

endmntent() 的功用是关闭打开的文件。这不能只是呼叫 fclose() 而已,因为可能还有其它与FILE * 有关的内部资料结构需要清理。

hasmntopt() 是个比较特殊的函式。它会扫描第一个参数所传入的struct mntent,找出它的挂载选项是否符合第二个引数。假如找到选项就传回符合的子字符串的位址;否则传回NULL。

/etc/fstab、/etc/mtab 和 /proc/mounts 其中任何一个, 都可以在程序中使用 getmntent() 这组函数来读取

eg:

   #include <mntent.h>

    struct mntent* mnt;
    FILE* fp;

    fp = setmntent("/dev/mmc/mmcblk0", "r");
    if ( !fp )
    {

        return FALSE;
    }

if(mnt=getmntent(fp) )
{

#if 1
     printf("woosoori[%s:%d] mnt->mnt_fsname=%s\n",__FUNCTION__,__LINE__, mnt->mnt_fsname);
   printf("woosoori[%s:%d] mnt->mnt_dir=%s\n",__FUNCTION__,__LINE__, mnt->mnt_dir);
   printf("woosoori[%s:%d]mnt->mnt_type=%s\n",__FUNCTION__,__LINE__,mnt->mnt_type);
   printf("woosoori[%s:%d] mnt->mnt_opts=%s\n",__FUNCTION__,__LINE__, mnt->mnt_opts);
   printf("woosoori[%s:%d]mnt->mnt_freq=%d\n",__FUNCTION__,__LINE__,mnt->mnt_freq);  
   printf("woosoori[%s:%d]mnt->mnt_passno=%d\n",__FUNCTION__,__LINE__,mnt->mnt_passno);  
#endif
     endmntent(fp);
     return TRUE;
}


#include <libubox/uloop.h> #include <libubox/ustream.h> #include <libubox/utils.h> #include <libubus.h> #include <json/json.h> #include <libubox/blobmsg_json.h> #include <time.h> #include <sys/time.h> #include <signal.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> //malloc free #include <string.h> //memset #include <arpa/inet.h> //ntohl #include <stdio.h> #include <dirent.h> #include <mntent.h> #include <sys/statfs.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <curl/curl.h> #include <pthread.h> #include "nvrwork_ubus.h" #include "tp_type.h" #include <termios.h> #include <linux/input.h> #include <linux/uinput.h> #include <errno.h> #include <linux/hdreg.h> #include <linux/types.h> #include <fcntl.h> #include <sys/ioctl.h> #include"trace_alloc.h" static struct ubus_context * media_ctx = NULL; static const char * wm_path; typedef struct _TEST_STRUCT{ int val; }TEST_STRUCT; static int nvrwork_trigger_mem_leak(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { int *ptr = (int *)malloc(sizeof(int) * 100); memset(ptr, 1, (sizeof(int) * 100)); printf("Memory leak callback.\n"); return OK; } static int nvrwork_trigger_access_nullptr(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { TEST_STRUCT *ts = NULL; ts->val = 1; int value = ts->val; printf("%d\n", value); printf("Access nullptr callback.\n"); return OK; } static int nvrwork_trigger_trace_alloc(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { trace_alloc_debug(); return OK; } static struct ubus_method nvrwork_methods[] = { {.name = "mem_leak", .handler = nvrwork_trigger_mem_leak}, {.name = "access_nullptr", .handler = nvrwork_trigger_access_nullptr}, {.name = "trace_alloc", .handler = nvrwork_trigger_trace_alloc}, }; static struct ubus_object_type nvrwork_obj_type = UBUS_OBJECT_TYPE("nvrwork", nvrwork_methods); static struct ubus_object nvrwork_obj = { .name = "nvrwork", .type = &nvrwork_obj_type, .methods = nvrwork_methods, .n_methods = ARRAY_SIZE(nvrwork_methods), }; static void ubus_add_fd(void) { ubus_add_uloop(media_ctx); #ifdef FD_CLOEXEC fcntl(media_ctx->sock.fd, F_SETFD, fcntl(media_ctx->sock.fd, F_GETFD) | FD_CLOEXEC); #endif } static void ubus_reconn_timer(struct uloop_timeout *timeout) { static struct uloop_timeout retry = { .cb = ubus_reconn_timer, }; int t = 2; if (ubus_reconnect(media_ctx, wm_path) != 0) { uloop_timeout_set(&retry, t * 1000); return; } ubus_add_fd(); } static void ubus_connection_lost(struct ubus_context *ctx) { ubus_reconn_timer(NULL); } int nvrwork_ubus_init(const char *path) { uloop_init(); wm_path = path; media_ctx = ubus_connect(NULL); if (!media_ctx) { printf("ubus connect failed\n"); return ERROR; } printf("connected as %08x\n", media_ctx->local_id); media_ctx->connection_lost = ubus_connection_lost; ubus_add_fd(); if (ubus_add_object(media_ctx, &nvrwork_obj) != 0) { printf("ubus add obj failed\n"); return ERROR; } return OK; } void nvrwork_ubus_done(void) { if (media_ctx) ubus_free(media_ctx); } int real_main(void * arg) { char * path = (char *)arg; if (ERROR == nvrwork_ubus_init(path)) { printf("ubus connect failed!\n"); return ERROR; } trace_alloc_set_threshold(100); uloop_run(); nvrwork_ubus_done(); return OK; } int main(int argc, char * argv[]) { real_main(NULL); return OK; } 分析一下这个代码 我要写一个新的模块 要模仿一下这个模块代码
最新发布
09-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值