File: umproc.c

package info (click to toggle)
umview 0.8.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 5,472 kB
  • sloc: ansic: 67,309; sh: 11,160; ruby: 914; makefile: 424; python: 141
file content (219 lines) | stat: -rw-r--r-- 5,054 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*   This is part of um-ViewOS
 *   The user-mode implementation of OSVIEW -- A Process with a View
 *
 *   UMPROC: /proc management
 *
 *   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.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mount.h>

#include <config.h>
#include "module.h"

#ifndef __UMPROC_DEBUG_LEVEL__
#define __UMPROC_DEBUG_LEVEL__ 0
#endif

#ifdef __UMPROC_DEBUG__
#define PRINTDEBUG(level,args...) printdebug(level, __FILE__, __LINE__, __func__, args)
#else
#define PRINTDEBUG(level,args...)
#endif

#define PROC_MOUNTS 1
static struct service s;
VIEWOS_SERVICE(s)

struct fileinfo {
	loff_t pos;        /* file offset */
	loff_t size;        /* file size */
	int flags;
	char *path;
	char *buf;
	struct umproc *umproc;
};

struct umproc {
	int tag;
};

struct umproc proc_mounts={PROC_MOUNTS};

static void fill_proc_mounts(struct fileinfo *ft)
{
	size_t size;
	FILE *f=open_memstream(&(ft->buf),&size);
	int fd=open("/proc/mounts",O_RDONLY);
	if (fd>=0) {
		char *buf[128];
		int n;
		while ((n=read(fd,buf,128)) > 0)
			fwrite(buf,n,1,f);
		close(fd);
	}
	ht_tab_getmtab(f);
	fclose(f);
	ft->size=size;
}

static long umproc_open(char *path, int flags, mode_t mode)
{
	struct umproc *mh = um_mod_get_private_data();
	assert(mh);
	int fd = addfiletab(sizeof(struct fileinfo));
	struct fileinfo *ft=getfiletab(fd);
	ft->pos = 0;
	ft->flags = flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
	ft->path=strdup(path);
	//printk("%d %lld %s\n",fd,ft->pos,ft->path);
	ft->umproc=mh;
	ft->buf=NULL;
	switch (mh->tag) {
		case PROC_MOUNTS:
			fill_proc_mounts(ft);
			break;
	}
	return fd;
}

static long umproc_close(int fd)
{
	struct fileinfo *ft=getfiletab(fd);
	if (ft->buf != NULL)
		free(ft->buf);
	free(ft->path);
	delfiletab(fd);
	return 0;
}

static long umproc_read(int fd, char *buf, size_t count)
{
	int rv;
	struct fileinfo *ft=getfiletab(fd);
	//printk("READIN %d c%d p%lld s%lld \n",rv,
	//count, ft->pos, ft->size);
	for (rv=0; rv< count; rv++) {
		if (ft->pos > ft->size)
			break;
		if (ft->buf[ft->pos] == 0)
			break;
		buf[rv]=ft->buf[ft->pos];
		ft->pos++;
	}
	//printk("READ %d c%d p%lld s%lld %s\n",rv,
	//	count, ft->pos, ft->size, buf);
	return rv;
}

static void setstat64(struct stat64 *buf64, int isdir)
{
	memset(buf64,0,sizeof(struct stat64));
	buf64->st_mode=S_IFREG | 0444;
}

static long umproc_lstat64(char *path, struct stat64 *buf64)
{
	struct umproc *mh = um_mod_get_private_data();
	assert(mh);
	//printk("stat64 %s %p\n",path,fse);
	setstat64(buf64,0);
	return 0;
}

/* TODO management of fcntl */
static long umproc_fcntl64(int fd, int cmd, void *arg)
{
	//print2("umproc_fcntl64\n");
	errno=0;
	return 0;
}

static long umproc_fsync(int fd, int cmd, void *arg)
{
	//print2("umproc_fcntl64\n");
	errno=0;
	return 0;
}

static long umproc_access(char *path, int mode)
{
	//struct umproc *mh = searchproc(path,SUBSTR);
	struct umproc *mh = um_mod_get_private_data();
	assert(mh);
	return 0;
}

static loff_t umproc_lseek(int fd, off_t offset, int whence)
{
	struct fileinfo *ft=getfiletab(fd);
	switch (whence) {
		case SEEK_SET: ft->pos=offset; break;
		case SEEK_CUR: ft->pos+=offset; break;
		case SEEK_END: ft->pos=strlen(ft->buf)+offset; break;
	}
	if (ft->pos < 0) ft->pos=0;
	if (ft->pos > ft->size) ft->pos=ft->size;
	return ft->pos;
}

void *viewos_init(char *args)
{
	return ht_tab_pathadd(CHECKPATH,"none","/proc/mounts","proc",MS_GHOST,"ro",&s,0,NULL,&proc_mounts);
}

void viewos_fini(void *data)
{
	struct ht_elem *proc_ht=data;
	ht_tab_del(proc_ht);
}

	static void
	__attribute__ ((constructor))
init (void)
{
	printk(KERN_NOTICE "umproc init\n");
	s.name="umproc";
	s.description="/proc virtualization";
	s.syscall=(sysfun *)calloc(scmap_scmapsize,sizeof(sysfun));
	s.socket=(sysfun *)calloc(scmap_sockmapsize,sizeof(sysfun));
	SERVICESYSCALL(s, open, umproc_open);
	SERVICESYSCALL(s, read, umproc_read);
	SERVICESYSCALL(s, close, umproc_close);
	SERVICESYSCALL(s, lstat64, umproc_lstat64);
	SERVICESYSCALL(s, fcntl, umproc_fcntl64);
	SERVICESYSCALL(s, fsync, umproc_fsync);
	SERVICESYSCALL(s, access, umproc_access);
	SERVICESYSCALL(s, lseek, umproc_lseek);
}

	static void
	__attribute__ ((destructor))
fini (void)
{
	free(s.syscall);
	free(s.socket);
	printk(KERN_NOTICE "umproc fini\n");
}