libfuse: fuse-3.18.2/example/printcap.c Source File

libfuse

printcap.c
Go to the documentation of this file.
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2017 Nikolaus Rath <Nikolaus@rath.org>
4
5 This program can be distributed under the terms of the GNU GPLv2.
6 See the file GPL2.txt.
7*/
8
22#define FUSE_USE_VERSION 31
23
24#include <fuse_lowlevel.h>
25#include <stdio.h>
26#include <unistd.h>
27#include <string.h>
28#include <stdlib.h>
29
30struct fuse_session *se;
31
32// Define a structure to hold capability information
33struct cap_info {
34 uint64_t flag;
35 const char *name;
36};
37
38// Define an array of all capabilities
39static const struct cap_info capabilities[] = {
40 {FUSE_CAP_ASYNC_READ, "FUSE_CAP_ASYNC_READ"},
41 {FUSE_CAP_POSIX_LOCKS, "FUSE_CAP_POSIX_LOCKS"},
42 {FUSE_CAP_ATOMIC_O_TRUNC, "FUSE_CAP_ATOMIC_O_TRUNC"},
43 {FUSE_CAP_EXPORT_SUPPORT, "FUSE_CAP_EXPORT_SUPPORT"},
44 {FUSE_CAP_DONT_MASK, "FUSE_CAP_DONT_MASK"},
45 {FUSE_CAP_SPLICE_MOVE, "FUSE_CAP_SPLICE_MOVE"},
46 {FUSE_CAP_SPLICE_READ, "FUSE_CAP_SPLICE_READ"},
47 {FUSE_CAP_SPLICE_WRITE, "FUSE_CAP_SPLICE_WRITE"},
48 {FUSE_CAP_FLOCK_LOCKS, "FUSE_CAP_FLOCK_LOCKS"},
49 {FUSE_CAP_IOCTL_DIR, "FUSE_CAP_IOCTL_DIR"},
50 {FUSE_CAP_AUTO_INVAL_DATA, "FUSE_CAP_AUTO_INVAL_DATA"},
51 {FUSE_CAP_READDIRPLUS, "FUSE_CAP_READDIRPLUS"},
52 {FUSE_CAP_READDIRPLUS_AUTO, "FUSE_CAP_READDIRPLUS_AUTO"},
53 {FUSE_CAP_ASYNC_DIO, "FUSE_CAP_ASYNC_DIO"},
54 {FUSE_CAP_WRITEBACK_CACHE, "FUSE_CAP_WRITEBACK_CACHE"},
55 {FUSE_CAP_NO_OPEN_SUPPORT, "FUSE_CAP_NO_OPEN_SUPPORT"},
56 {FUSE_CAP_PARALLEL_DIROPS, "FUSE_CAP_PARALLEL_DIROPS"},
57 {FUSE_CAP_POSIX_ACL, "FUSE_CAP_POSIX_ACL"},
58 {FUSE_CAP_CACHE_SYMLINKS, "FUSE_CAP_CACHE_SYMLINKS"},
59 {FUSE_CAP_NO_OPENDIR_SUPPORT, "FUSE_CAP_NO_OPENDIR_SUPPORT"},
60 {FUSE_CAP_EXPLICIT_INVAL_DATA, "FUSE_CAP_EXPLICIT_INVAL_DATA"},
61 {FUSE_CAP_EXPIRE_ONLY, "FUSE_CAP_EXPIRE_ONLY"},
62 {FUSE_CAP_SETXATTR_EXT, "FUSE_CAP_SETXATTR_EXT"},
63 {FUSE_CAP_HANDLE_KILLPRIV, "FUSE_CAP_HANDLE_KILLPRIV"},
64 {FUSE_CAP_HANDLE_KILLPRIV_V2, "FUSE_CAP_HANDLE_KILLPRIV_V2"},
65 {FUSE_CAP_DIRECT_IO_ALLOW_MMAP, "FUSE_CAP_DIRECT_IO_ALLOW_MMAP"},
66 {FUSE_CAP_NO_EXPORT_SUPPORT, "FUSE_CAP_NO_EXPORT_SUPPORT"},
67 {FUSE_CAP_PASSTHROUGH, "FUSE_CAP_PASSTHROUGH"},
68 // Add any new capabilities here
69 {0, NULL} // Sentinel to mark the end of the array
70};
71
72static void print_capabilities(struct fuse_conn_info *conn)
73{
74 printf("Capabilities:\n");
75 for (const struct cap_info *cap = capabilities; cap->name != NULL; cap++) {
76 if (fuse_get_feature_flag(conn, cap->flag)) {
77 printf("\t%s\n", cap->name);
78 }
79 }
80}
81
82static void pc_init(void *userdata, struct fuse_conn_info *conn)
83{
84 (void) userdata;
85
86 printf("Protocol version: %d.%d\n", conn->proto_major,
87 conn->proto_minor);
88 print_capabilities(conn);
89 fuse_session_exit(se);
90}
91
92
93static const struct fuse_lowlevel_ops pc_oper = {
94 .init = pc_init,
95};
96
97int main(int argc, char **argv)
98{
99 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
100 char *mountpoint;
101 int ret = -1;
102
103 mountpoint = strdup("/tmp/fuse_printcap_XXXXXX");
104 if(mkdtemp(mountpoint) == NULL) {
105 perror("mkdtemp");
106 return 1;
107 }
108
109 printf("FUSE library version %s\n", fuse_pkgversion());
110 fuse_lowlevel_version();
111
112 se = fuse_session_new(&args, &pc_oper,
113 sizeof(pc_oper), NULL);
114 if (se == NULL)
115 goto err_out1;
116
117 if (fuse_set_signal_handlers(se) != 0)
118 goto err_out2;
119
120 if (fuse_session_mount(se, mountpoint) != 0)
121 goto err_out3;
122
123 ret = fuse_session_loop(se);
124
125 fuse_session_unmount(se);
126err_out3:
127 fuse_remove_signal_handlers(se);
128err_out2:
129 fuse_session_destroy(se);
130err_out1:
131 rmdir(mountpoint);
132 free(mountpoint);
133 fuse_opt_free_args(&args);
134
135 return ret ? 1 : 0;
136}
FUSE_CAP_IOCTL_DIR
#define FUSE_CAP_IOCTL_DIR
Definition fuse_common.h:259
FUSE_CAP_DONT_MASK
#define FUSE_CAP_DONT_MASK
Definition fuse_common.h:214
FUSE_CAP_HANDLE_KILLPRIV
#define FUSE_CAP_HANDLE_KILLPRIV
Definition fuse_common.h:388
FUSE_CAP_AUTO_INVAL_DATA
#define FUSE_CAP_AUTO_INVAL_DATA
Definition fuse_common.h:281
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:140
FUSE_CAP_HANDLE_KILLPRIV_V2
#define FUSE_CAP_HANDLE_KILLPRIV_V2
Definition fuse_common.h:405
FUSE_CAP_SPLICE_READ
#define FUSE_CAP_SPLICE_READ
Definition fuse_common.h:239
FUSE_CAP_PARALLEL_DIROPS
#define FUSE_CAP_PARALLEL_DIROPS
Definition fuse_common.h:360
FUSE_CAP_WRITEBACK_CACHE
#define FUSE_CAP_WRITEBACK_CACHE
Definition fuse_common.h:337
FUSE_CAP_EXPIRE_ONLY
#define FUSE_CAP_EXPIRE_ONLY
Definition fuse_common.h:472
FUSE_CAP_ATOMIC_O_TRUNC
#define FUSE_CAP_ATOMIC_O_TRUNC
Definition fuse_common.h:194
FUSE_CAP_ASYNC_READ
#define FUSE_CAP_ASYNC_READ
Definition fuse_common.h:177
FUSE_CAP_SPLICE_WRITE
#define FUSE_CAP_SPLICE_WRITE
Definition fuse_common.h:222
FUSE_CAP_CACHE_SYMLINKS
#define FUSE_CAP_CACHE_SYMLINKS
Definition fuse_common.h:418
FUSE_CAP_POSIX_ACL
#define FUSE_CAP_POSIX_ACL
Definition fuse_common.h:379
FUSE_CAP_EXPORT_SUPPORT
#define FUSE_CAP_EXPORT_SUPPORT
Definition fuse_common.h:206
FUSE_CAP_POSIX_LOCKS
#define FUSE_CAP_POSIX_LOCKS
Definition fuse_common.h:185
FUSE_CAP_EXPLICIT_INVAL_DATA
#define FUSE_CAP_EXPLICIT_INVAL_DATA
Definition fuse_common.h:456
FUSE_CAP_READDIRPLUS_AUTO
#define FUSE_CAP_READDIRPLUS_AUTO
Definition fuse_common.h:317
FUSE_CAP_NO_OPENDIR_SUPPORT
#define FUSE_CAP_NO_OPENDIR_SUPPORT
Definition fuse_common.h:433
FUSE_CAP_ASYNC_DIO
#define FUSE_CAP_ASYNC_DIO
Definition fuse_common.h:328
fuse_get_feature_flag
bool fuse_get_feature_flag(struct fuse_conn_info *conn, uint64_t flag)
Definition fuse_lowlevel.c:2061
FUSE_CAP_PASSTHROUGH
#define FUSE_CAP_PASSTHROUGH
Definition fuse_common.h:500
FUSE_CAP_DIRECT_IO_ALLOW_MMAP
#define FUSE_CAP_DIRECT_IO_ALLOW_MMAP
Definition fuse_common.h:488
FUSE_CAP_NO_OPEN_SUPPORT
#define FUSE_CAP_NO_OPEN_SUPPORT
Definition fuse_common.h:352
FUSE_CAP_READDIRPLUS
#define FUSE_CAP_READDIRPLUS
Definition fuse_common.h:289
fuse_pkgversion
const char * fuse_pkgversion(void)
Definition fuse.c:5211
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:187
FUSE_CAP_SETXATTR_EXT
#define FUSE_CAP_SETXATTR_EXT
Definition fuse_common.h:479
FUSE_CAP_SPLICE_MOVE
#define FUSE_CAP_SPLICE_MOVE
Definition fuse_common.h:230
FUSE_CAP_NO_EXPORT_SUPPORT
#define FUSE_CAP_NO_EXPORT_SUPPORT
Definition fuse_common.h:508
FUSE_CAP_FLOCK_LOCKS
#define FUSE_CAP_FLOCK_LOCKS
Definition fuse_common.h:252
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition fuse_lowlevel.c:3010
fuse_session_exit
void fuse_session_exit(struct fuse_session *se)
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition fuse_loop.c:19
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition fuse_lowlevel.c:3539
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition fuse_lowlevel.c:3473
fuse_lowlevel_version
void fuse_lowlevel_version(void)
Definition fuse_lowlevel.c:2993
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
fuse_args
Definition fuse_opt.h:109
fuse_args::argc
int argc
Definition fuse_opt.h:111
fuse_args::argv
char ** argv
Definition fuse_opt.h:114
fuse_conn_info
Definition fuse_common.h:538
fuse_conn_info::proto_major
uint32_t proto_major
Definition fuse_common.h:542
fuse_conn_info::proto_minor
uint32_t proto_minor
Definition fuse_common.h:547
fuse_lowlevel_ops
Definition fuse_lowlevel.h:206
fuse_lowlevel_ops::init
void(* init)(void *userdata, struct fuse_conn_info *conn)
Definition fuse_lowlevel.h:223