xref: /openbsd-src/usr.bin/sndiod/file.h (revision 7b6392009e6e5a7f8e494c162a4d259ea5e13a62)
1 /*	$OpenBSD: file.h,v 1.7 2024/12/20 07:35:56 ratchov Exp $	*/
2 /*
3  * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef FILE_H
18 #define FILE_H
19 
20 #include <sys/types.h>
21 
22 struct file;
23 struct pollfd;
24 
25 struct timo {
26 	struct timo *next;
27 	unsigned int val;		/* time to wait before the callback */
28 	unsigned int set;		/* true if the timeout is set */
29 	void (*cb)(void *arg);		/* routine to call on expiration */
30 	void *arg;			/* argument to give to 'cb' */
31 };
32 
33 struct fileops {
34 	char *name;
35 	int (*pollfd)(void *, struct pollfd *);
36 	int (*revents)(void *, struct pollfd *);
37 	/*
38 	 * we have to handle POLLIN and POLLOUT events
39 	 * in separate handles, since handling POLLIN can
40 	 * close the file, and continuing (to handle POLLOUT)
41 	 * would make use of the free()'ed file structure
42 	 */
43 	void (*in)(void *);
44 	void (*out)(void *);
45 	void (*hup)(void *);
46 };
47 
48 struct file {
49 	struct file *next;		/* next in file_list */
50 	struct fileops *ops;		/* event handlers */
51 	void *arg;			/* argument to event handlers */
52 #define FILE_INIT	0		/* ready */
53 #define FILE_ZOMB	1		/* closed, but not free()d yet */
54 	unsigned int state;		/* one of above */
55 	unsigned int max_nfds;		/* max number of descriptors */
56 	unsigned int nfds;		/* number of descriptors polled */
57 	char *name;			/* for debug purposes */
58 };
59 
60 extern struct file *file_list;
61 extern int file_slowaccept;
62 
63 #ifdef DEBUG
64 extern long long file_wtime, file_utime;
65 #endif
66 
67 void timo_set(struct timo *, void (*)(void *), void *);
68 void timo_add(struct timo *, unsigned int);
69 void timo_del(struct timo *);
70 
71 void filelist_init(void);
72 void filelist_done(void);
73 size_t filelist_fmt(char *, size_t, struct pollfd *, int);
74 
75 struct file *file_new(struct fileops *, void *, char *, unsigned int);
76 void file_del(struct file *);
77 
78 int file_poll(void);
79 
80 #endif /* !defined(FILE_H) */
81