File: control.c

package info (click to toggle)
vbox3 0.1.7.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 660 kB
  • ctags: 273
  • sloc: ansic: 3,454; sh: 1,831; makefile: 119; tcl: 22
file content (143 lines) | stat: -rw-r--r-- 4,095 bytes parent folder | download | duplicates (3)
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
/*
** $Id: control.c,v 1.6 2001/01/14 13:50:20 pape Exp $
**
** Copyright 1996-1998 Michael 'Ghandi' Herold <michael@abadonna.mayn.de>
*/

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>

#include "control.h"
#include "stringutils.h"
#include "log.h"

/** Variablen ************************************************************/

static unsigned char ctrlpathname[PATH_MAX + 1];
static unsigned char ctrllastline[VBOX_CTRL_MAX_RCLINE + 1];

/*************************************************************************/
/** ctrl_exists():	Untersucht ob eine Kontrolldatei existiert und		**/
/**						gibt deren Inhalt zurck.									**/
/*************************************************************************/
/** => home				Homeverzeichnis des Benutzers.							**/
/** => name				Name der Kontrolldatei (ohne vboxctrl-).				**/
/**																							**/
/** <=					NULL wenn die Datei nicht gelesen werden konnte		**/
/**						oder ein Zeiger auf deren Initstring.					**/
/*************************************************************************/

char *ctrl_exists(unsigned char *home, unsigned char *name, unsigned char *ttyd)
{
	FILE *cptr;
	char *stop;

	if (!ttyd)
		printstring(ctrlpathname, "%s/vboxctrl-%s", home, name);
	else
		printstring(ctrlpathname, "%s/vboxctrl-%s-%s", home, name, ttyd);

	if ((cptr = fopen(ctrlpathname, "r")))
	{
		if (fgets(ctrllastline, VBOX_CTRL_MAX_RCLINE, cptr))
		{
			ctrllastline[strlen(ctrllastline) - 1] = '\0';

			if ((stop = index(ctrllastline, '\r'))) *stop = '\0';

			fclose(cptr);
			return(ctrllastline);
		}

		fclose(cptr);
	}

	return(NULL);
}

/*************************************************************************/
/** ctrl_create():	Erzeugt eine Kontrolldatei und schreibt den Init-	**/
/**						string.															**/
/*************************************************************************/
/** => home				Homeverzeichnis des Benutzers.							**/
/** => name				Name der Kontrolldatei (ohne vboxctrl-).				**/
/** => init				Initstring.														**/
/**																							**/
/** <=					0 wenn die Datei erzeugt werden konnte oder -1 bei	**/
/**						einem Fehler.													**/
/*************************************************************************/

int ctrl_create(unsigned char *home, unsigned char *name, unsigned char *ttyd, unsigned char *init)
{
	FILE *cptr = NULL;
	int	loop = 5;

	if (!ttyd)
		printstring(ctrlpathname, "%s/vboxctrl-%s", home, name);
	else
		printstring(ctrlpathname, "%s/vboxctrl-%s-%s", home, name, ttyd);

	while (loop > 0)
	{
		log_line(LOG_D, "Creating control \"vboxctrl-%s:%s\" (%s)...\n", name, init, (char *)ttyd ? (char *)ttyd : "global");

		if ((cptr = fopen(ctrlpathname, "w")))
		{
			fprintf(cptr, "%s\n", init);
			fclose(cptr);
			return(0);
		}

		usleep(500);
		
		loop--;
	}

	log_line(LOG_E, "Can't create \"%s\".\n", ctrlpathname);

	return(-1);
}

/*************************************************************************/
/** ctrl_remove():	Lscht eine Kontrolldatei.									**/
/*************************************************************************/
/** => home				Homeverzeichnis des Benutzers.							**/
/** => name				Name der Kontrolldatei (ohne vboxctrl-).				**/
/**																							**/
/** <=					0 wenn die Datei gelscht werden konnte oder -1		**/
/**						bei einem Fehler.												**/
/*************************************************************************/

int ctrl_remove(unsigned char *home, unsigned char *name, unsigned char *ttyd)
{
	int loop = 5;

	if (!ttyd)
		printstring(ctrlpathname, "%s/vboxctrl-%s", home, name);
	else
		printstring(ctrlpathname, "%s/vboxctrl-%s-%s", home, name, ttyd);

	while (loop > 0)
	{
		log_line(LOG_D, "Removing control \"vboxctrl-%s\" (%s)...\n", name, (char *)ttyd ? (char *)ttyd : "global");

		if (remove(ctrlpathname) == 0) return(0);

		if (errno == ENOENT) return(0);

		usleep(500);

		loop--;
	}	

	log_line(LOG_E, "Can't remove \"%s\".\n", ctrlpathname);

	return(-1);
}