Consolidated patch of previously merged CYGWIN support

The combined results of merge request #49 without that
overhead plus distortion in this repository's history.

Prototyped-by: Wayne Porter <wporter82@gmail.com>
This commit is contained in:
Wayne Porter
2017-08-30 15:15:15 -05:00
committed by Craig Small
parent 854e2c5528
commit 53e101452f
9 changed files with 187 additions and 19 deletions

View File

@ -203,15 +203,25 @@ int main(int argc, char *argv[]){
/* Try to guess the device name (useful until /proc/PID/tty is added) */
static int guess_name(char *restrict const buf, unsigned maj, unsigned min){
struct stat sbuf;
#ifndef __CYGWIN__
int t0, t1;
#endif
unsigned tmpmin = min;
switch(maj){
case 3: /* /dev/[pt]ty[p-za-o][0-9a-z] is 936 */
if(tmpmin > 255) return 0; // should never happen; array index protection
#ifdef __CYGWIN__
sprintf(buf, "dev/cons%d", tmpmin);
/* Skip stat call. The reason is that cons devices are local to
the processes running in that console. Calling stat from another
console or pty will return -1. */
return 1;
#else
t0 = "pqrstuvwxyzabcde"[tmpmin>>4];
t1 = "0123456789abcdef"[tmpmin&0x0f];
sprintf(buf, "/dev/tty%c%c", t0, t1);
#endif
break;
case 4:
if(min<64){
@ -236,8 +246,12 @@ static int guess_name(char *restrict const buf, unsigned maj, unsigned min){
case 78: sprintf(buf, "/dev/ttyM%d", min); break; /* conflict */
case 105: sprintf(buf, "/dev/ttyV%d", min); break;
case 112: sprintf(buf, "/dev/ttyM%d", min); break; /* conflict */
#ifdef __CYGWIN__
case 136: sprintf(buf, "/dev/pty%d", min); break;
#else
/* 136 ... 143 are /dev/pts/0, /dev/pts/1, /dev/pts/2 ... */
case 136 ... 143: sprintf(buf, "/dev/pts/%d", min+(maj-136)*256); break;
#endif
case 148: sprintf(buf, "/dev/ttyT%d", min); break;
case 154: sprintf(buf, "/dev/ttySR%d", min); break;
case 156: sprintf(buf, "/dev/ttySR%d", min+256); break;
@ -285,6 +299,30 @@ static int link_name(char *restrict const buf, unsigned maj, unsigned min, int p
return 1;
}
#ifdef __CYGWIN__
/* Cygwin keeps the name to the controlling tty in a virtual file called
/proc/PID/ctty, including a trailing LF (sigh). */
static int ctty_name(char *restrict const buf, int pid) {
char path[32];
FILE *fp;
char *lf;
sprintf (path, "/proc/%d/ctty", pid); /* often permission denied */
fp = fopen (path, "r");
if (!fp)
return 0;
if (!fgets (buf,TTY_NAME_SIZE,fp))
{
fclose (fp);
return 0;
}
fclose (fp);
lf = strchr (buf, '\n');
if (lf)
*lf = (lf == buf ? '?' : '\0');
return 1;
}
#endif
/* number --> name */
unsigned dev_to_tty(char *restrict ret, unsigned chop, dev_t dev_t_dev, int pid, unsigned int flags) {
static char buf[TTY_NAME_SIZE];
@ -293,6 +331,9 @@ unsigned dev_to_tty(char *restrict ret, unsigned chop, dev_t dev_t_dev, int pid,
unsigned i = 0;
int c;
if(dev == 0u) goto no_tty;
#ifdef __CYGWIN__
if( ctty_name(tmp, pid )) goto abbrev;
#endif
if(driver_name(tmp, MAJOR_OF(dev), MINOR_OF(dev) )) goto abbrev;
if( link_name(tmp, MAJOR_OF(dev), MINOR_OF(dev), pid, "fd/2" )) goto abbrev;
if( guess_name(tmp, MAJOR_OF(dev), MINOR_OF(dev) )) goto abbrev;

View File

@ -81,7 +81,7 @@
// Like HIDDEN, but for an alias that gets created.
// In gcc-3.2 there is an alias+hidden conflict.
// Many will have patched this bug, but oh well.
#if ( __GNUC__ == 3 && __GNUC_MINOR__ > 2 ) || __GNUC__ > 3
#if (( __GNUC__ == 3 && __GNUC_MINOR__ > 2 ) || __GNUC__ > 3) && !defined(__CYGWIN__)
#define HIDDEN_ALIAS(x) extern __typeof(x) x##_direct __attribute__((alias(#x),visibility("hidden")))
#else
#define HIDDEN_ALIAS(x) extern __typeof(x) x##_direct __attribute__((alias(#x)))

View File

@ -28,6 +28,9 @@
#include <unistd.h>
#include <fcntl.h>
#ifdef __CYGWIN__
#include <sys/param.h>
#endif
#include "alloc.h"
#include "version.h"
#include "sysinfo.h" /* include self to verify prototypes */
@ -36,7 +39,9 @@
#include <netinet/in.h> /* htons */
#endif
#ifndef __CYGWIN__
#include <link.h>
#endif
#include <elf.h>
long smp_num_cpus; /* number of CPUs */
@ -91,7 +96,9 @@ static char buf[8192];
#define SET_IF_DESIRED(x,y) do{ if(x) *(x) = (y); }while(0)
/* return minimum of two values */
#ifndef __CYGWIN__
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#endif
/***********************************************************************/
int uptime(double *restrict uptime_secs, double *restrict idle_secs) {
@ -224,6 +231,7 @@ static void old_Hertz_hack(void){
case 247 ... 252 : Hertz = 250; break;
case 253 ... 260 : Hertz = 256; break;
case 393 ... 408 : Hertz = 400; break; /* normal << 2 */
case 410 ... 600 : Hertz = 500; break; /* SMP WinNT */
case 790 ... 808 : Hertz = 800; break; /* normal << 3 */
case 990 ... 1010 : Hertz = 1000; break; /* ARM */
case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */
@ -254,6 +262,9 @@ extern char** environ;
static unsigned long find_elf_note(unsigned long type)
{
#ifdef __CYGWIN__
return NOTE_NOT_FOUND;
#else
ElfW(auxv_t) auxv_struct;
ElfW(auxv_t) *auxv_temp;
FILE *fd;
@ -313,6 +324,7 @@ static unsigned long find_elf_note(unsigned long type)
auxv = NULL;
}
return ret_val;
#endif
}
int have_privs;
@ -350,6 +362,11 @@ static void init_libproc(void){
Hertz = 100;
return;
#endif /* __FreeBSD__ */
#ifdef __CYGWIN__
// On Cygwin we can rely on the HZ value given in sys/param.h
Hertz = (unsigned long long)HZ; /* <sys/param.h> */
return;
#endif
old_Hertz_hack();
}