-
Notifications
You must be signed in to change notification settings - Fork 0
/
SysDepLinux.cpp
184 lines (146 loc) · 4.78 KB
/
SysDepLinux.cpp
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
/*
* XMail by Davide Libenzi (Intranet and Internet mail server)
* Copyright (C) 1999,..,2010 Davide Libenzi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <[email protected]>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SysDepUnix.h"
#include "AppDefines.h"
#define SINF_SIZE(s, f) (((SYS_INT64) (s).f) * (s).mem_unit)
int SysDepInitLibrary(void)
{
return 0;
}
void SysDepCleanupLibrary(void)
{
}
#ifndef SYS_USE_MMSENDFILE
int SysSendFile(SYS_SOCKET SockFD, char const *pszFileName, SYS_OFF_T llBaseOffset,
SYS_OFF_T llEndOffset, int iTimeout)
{
int iFileID = open(pszFileName, O_RDONLY);
if (iFileID == -1) {
ErrSetErrorCode(ERR_FILE_OPEN, pszFileName);
return ERR_FILE_OPEN;
}
SYS_OFF_T llFileSize = (SYS_OFF_T) lseek(iFileID, 0, SEEK_END);
lseek(iFileID, 0, SEEK_SET);
if (llEndOffset == -1)
llEndOffset = llFileSize;
/* Set send timeout */
socklen_t OptLenght = sizeof(struct timeval);
struct timeval oldTV;
struct timeval newTV;
if (getsockopt((int) SockFD, SOL_SOCKET, SO_SNDTIMEO, &oldTV, &OptLenght)) {
close(iFileID);
ErrSetErrorCode(ERR_GETSOCKOPT);
return ERR_GETSOCKOPT;
}
newTV.tv_sec = iTimeout;
newTV.tv_usec = 0;
setsockopt((int) SockFD, SOL_SOCKET, SO_SNDTIMEO, &newTV, sizeof(newTV));
/* Send the file */
size_t sSndBuffSize = MIN_TCP_SEND_SIZE;
time_t tStart;
while (llBaseOffset < llEndOffset) {
size_t sCurrSend = (size_t) Min((SYS_OFF_T) sSndBuffSize, llEndOffset - llBaseOffset);
off_t ulStartOffset = (off_t) llBaseOffset;
tStart = time(NULL);
size_t sSendSize = sendfile((int) SockFD, iFileID, &ulStartOffset, sCurrSend);
if (sSendSize != sCurrSend) {
setsockopt((int) SockFD, SOL_SOCKET, SO_SNDTIMEO, &oldTV, sizeof(oldTV));
close(iFileID);
ErrSetErrorCode(ERR_SENDFILE);
return ERR_SENDFILE;
}
if ((((time(NULL) - tStart) * K_IO_TIME_RATIO) < iTimeout) &&
(sSndBuffSize < MAX_TCP_SEND_SIZE))
sSndBuffSize = Min(sSndBuffSize * 2, MAX_TCP_SEND_SIZE);
llBaseOffset += sCurrSend;
}
setsockopt((int) SockFD, SOL_SOCKET, SO_SNDTIMEO, &oldTV, sizeof(oldTV));
close(iFileID);
return 0;
}
#else
int SysSendFile(SYS_SOCKET SockFD, char const *pszFileName, SYS_OFF_T llBaseOffset,
SYS_OFF_T llEndOffset, int iTimeout)
{
return SysSendFileMMap(SockFD, pszFileName, llBaseOffset, llEndOffset, iTimeout);
}
#endif
int SysSetThreadPriority(SYS_THREAD ThreadID, int iPriority)
{
ThrData *pTD = (ThrData *) ThreadID;
int iPolicy;
struct sched_param SchParam;
if (pthread_getschedparam(pTD->ThreadId, &iPolicy, &SchParam) != 0) {
ErrSetErrorCode(ERR_SET_THREAD_PRIORITY);
return ERR_SET_THREAD_PRIORITY;
}
int iMinPriority = sched_get_priority_min(iPolicy),
iMaxPriority = sched_get_priority_max(iPolicy),
iStdPriority = (iMinPriority + iMaxPriority) / 2;
switch (iPriority) {
case SYS_PRIORITY_NORMAL:
SchParam.sched_priority = iStdPriority;
break;
case SYS_PRIORITY_LOWER:
SchParam.sched_priority = iStdPriority - (iStdPriority - iMinPriority) / 3;
break;
case SYS_PRIORITY_HIGHER:
SchParam.sched_priority = iStdPriority + (iStdPriority - iMinPriority) / 3;
break;
}
if (pthread_setschedparam(pTD->ThreadId, iPolicy, &SchParam) != 0) {
ErrSetErrorCode(ERR_SET_THREAD_PRIORITY);
return ERR_SET_THREAD_PRIORITY;
}
return 0;
}
long SysGetTimeZone(void)
{
return (long) timezone;
}
int SysGetDiskSpace(char const *pszPath, SYS_INT64 *pTotal, SYS_INT64 *pFree)
{
struct statfs SFStat;
if (statfs(pszPath, &SFStat) != 0) {
ErrSetErrorCode(ERR_GET_DISK_SPACE_INFO);
return ERR_GET_DISK_SPACE_INFO;
}
*pTotal = (SYS_INT64) SFStat.f_bsize * (SYS_INT64) SFStat.f_blocks;
*pFree = (SYS_INT64) SFStat.f_bsize * (SYS_INT64) SFStat.f_bavail;
return 0;
}
int SysMemoryInfo(SYS_INT64 *pRamTotal, SYS_INT64 *pRamFree,
SYS_INT64 *pVirtTotal, SYS_INT64 *pVirtFree)
{
struct sysinfo SI;
if (sysinfo(&SI) < 0) {
ErrSetErrorCode(ERR_GET_MEMORY_INFO);
return ERR_GET_MEMORY_INFO;
}
*pRamTotal = SINF_SIZE(SI, totalram);
*pRamFree = SINF_SIZE(SI, freeram);
*pVirtTotal = SINF_SIZE(SI, totalswap) + SINF_SIZE(SI, totalram);
*pVirtFree = SINF_SIZE(SI, freeswap) + SINF_SIZE(SI, freeram);
return 0;
}