Cygwin: Difference between revisions

From miki
Jump to navigation Jump to search
Line 169: Line 169:
=== Building from source in Cygwin ===
=== Building from source in Cygwin ===
Building a package from source in Cygwin is unfortunately not an easy task. The steps are:
Building a package from source in Cygwin is unfortunately not an easy task. The steps are:
* Download the source package (easy)
# '''Download the source package'''
:The source package can be downloaded with <code>setup.exe</code>. The archive is found in directory <tt>/usr/src</tt>.
* Find the dependencies (hard/easy)
# '''Find the dependencies'''
* Find which commands to use (hard)
:Building a package requires that some extra development libraries be installed as well. These libraries are only needed for the build, and can be removed afterwards.
# '''Find which chain of commands to use'''


The source package can be downloaded with <code>setup.exe</code>. The archive is found in directory <tt>/usr/src</tt>.


The main problem I encountered is the total lack of documentation in the source package. The same applies regarding any administration task in Cygwin (like how to install a package manually, etc). Obviously the usual way of <tt>./configure; make && make install</tt> could work, but Cygwin often applies specific patches to packages. Also most packages have lots of configuration options. Clearly there must be some automated way to build the packages from the source.
The main problem I encountered is the total lack of documentation in the source package. The same applies regarding any administration task in Cygwin (like how to install a package manually, etc). Obviously the usual way of <tt>./configure; make && make install</tt> could work, but Cygwin often applies specific patches to packages. Also most packages have lots of configuration options. Clearly there must be some automated way to build the packages from the source.


The hint comes from the file with extension <tt>.cygport</tt> in the source package. See [[#cygport]] below.
The hint comes from the file with extension <tt>.cygport</tt> in the source package. See [[#cygport]] below.


=== Cygport ===
=== Cygport ===

Revision as of 16:31, 27 July 2011

References

  • Local Reference Cygwin documentation can be found at /usr/share/doc/cygwin-doc-1.4.

Packages

Creating a local setup.ini

Packages in Cygwin can be installed from repositories. The content of these repositories is described in a file setup.ini. Each package in the repository is made of one or more .bz2 files (the actual package) and a file setup.hint (spec) describing the content of the package. The setup.ini file of the repository is actually a compilation of all setup.hint files in the repository along with additional information. Cygwin apparently does not want to install a package from a single .hint file (or at least have not found how to do so). In that case you need to generate the setup.ini file. Here's an example how this can be done.

Example is based on package recode v3.6.1.

  • Download the package above, and create following directory structure within Cygwin Local Package Directory (where setup.ini location is already shown).
.
|-- http%3a%2f%2fftp....
|   ...
`-- local
    |-- release
    |   `-- recode
    |       |-- recode-3.6-1-src.tar.bz2
    |       |-- recode-3.6-1.tar.bz2
    |       |-- recode-devel
    |       |   |-- recode-devel-3.6-1.tar.bz2
    |       |   `-- setup.hint
    |       `-- setup.hint
    `-- setup.ini
  • Create the file ./local/setup.ini with the following content:
# File manually generated on 20081105.
# setup-timestamp and setup-version are completely fake...
setup-timestamp: 1214338445
setup-version: 2.573.2.2

@ recode
sdesc: "GNU recode utility"
ldesc: "The program recognizes or produces approx. 150 character sets and can convert almost any character set to almost  any other."
category: text
requires: cygwin libintl3
version: 3.6-1
install: release/recode/recode-3.6-1.tar.bz2 838836 c185a17d8e1eb964611d26cbfe9b0e05
source: release/recode/recode-3.6-1-src.tar.bz2 1731299 83b0f5940aa5d2a82355586b6d977bab
  • (in gray) First the header
  • (in black) Then entry giving the package name
  • (in red) Then a copy-paste from setup.hint file.
  • (in green) Then the version (derived from package name)
  • (in blue) And finally two entries for binary and source package, giving for each: location of .bz2 file, the file size in bytes, the md5 checksum (as given by md5sum).

When done, simply install the package by running Cygwin setup.exe, asking to Install from Local Directory. Cygwin will see the new file setup.ini, and proposes for install the packages listed inside.

Troubleshooting

Cygwin

  • Look for multiple copies of the cygwin DLL cygwin1.dll. Removes all copies that are not in the cygwin install directory (usually c:\cygwin).
  • List of interfering software:
    • NoMachine NX Client - installs a custom version of the Cygwin DLL which can cause multiple interferences:
      • Cygwin crashes when multiple instances are launched.
      • Running cygwin commands (like ls) in a NT command shell when another instance of cygwin is running creates application error (Memory cannot be "read").
      • setup.exe fails because it runs several instances of bash.
    • SysInternals' Process Monitor
      • All back-quoted commands or $() commands in scripts fail until Windows is restarted

Missing DLL using strace

strace can be used to check whether an application needs a .DLL but cannot find it. This error is not reported by default on cygwin, so an application may just leave without any message, giving the impression that it simply does nothing. strace on the other hand will report about the missing .DLL. To solve the problem, install the .DLL somewhere in your PATH, or edit the PATH to include the .DLL location.

Example ([1]):

$ strace /usr/local/sbin/authtest                   # This will open a dialog box to complain about DLL.
$ export PATH=$PATH:/usr/local/lib/bin
$ /usr/local/sbin/authtest ausername

Administration

Adding Users

Create the user in Windows User Management first, then use command mkpasswd to add it to Cygwin environment.

$ net user <username> <password> /add /yes
$ net localgroup <an_local_group> <username> /add
$ mkpasswd -l -u <username> >> /etc/passwd

Installing a Service

Reference: Cygwin inetutils-1.5 OLD-README

Use cygrunsrv to install a Cygwin process as a service in Windows. Some examples:

$ cygrunsrv -I inetd -d "CYGWIN inetd" -p /usr/sbin/inetd -a -D
$ cygrunsrv --install imapd --desc "Courier IMAP daemon" --disp "CYGWIN imapd" \
  --path /usr/local/sbin/imapd-service.sh --termsig QUIT --type auto --shutdown

By default this will install the service to run under account SYSTEM (LocalSystem), who is the user that has all the required permission up to Windows XP. For Windows 2003 Server or Windows Vista, see below.

Start/Stop the service

Manually starting and stopping is possible via

$ net start <service>
$ net stop <service>

Creating a Privileged User in Windows 2003, Windows Vista

Beginning with Windows 2003 Server and Windows Vista, the SYSTEM user is lacking the "Create a token object" right, which is required to change the user without providing the password. On these systems you have to create an admin user account with special additional user rights. You can use the Cygwin tool editrights or the local/domain security policy mmc snap in to set these rights:

Create a token object
Act as part of the operating system
Replace process level token
Increase quotas
Logon as a service

Get a Bash shell owned by another user

For instance, to open a Bash window as user localuser (you'll need to type in the user's password)

$ runas /user:localuser c:\cygwin\Cygwin.bat

Another method is to apply the same trick used to log as user SYSTEM (see below). This trick is even better since it doesn't require the user password, and hence would enable privilege escalation. However Windows security system prevents a process running as a user without providing the user password to connect to network shares.

Get a Bash shell owned by SYSTEM

This is a trick I found to easily open a Bash windows as user SYSTEM. First create a dummy service bashsystem:

$ cygrunsrv --install bashsystem --desc "Bash SYSTEM" --disp "CYGWIN bashsystem" --path /Cygwin.bat --type manual --interactive

Next, to open a Bash shell as user SYSTEM:

$ net start bashsystem

File permissions

  • Use getfacl and setfacl to set NT file permissions and access conditions.
  • To copy ACL from one file to another
% getfacl srcfile | setfacl -f - dstfile

Install SSHD

Reference: [2]

  • Install the packages:
cygrunsrv
openssh
  • Launch ssh host config:
ssh-host-config -y       # Yes to all question
  • When queried for CYGWIN variable, answer tty ntsec
  • Launch the service:
cygrunsrv -S sshd        # or 'net run sshd'
  • Optionally, open the SSH port (22) on the firewall

Development

Building from source in Cygwin

Building a package from source in Cygwin is unfortunately not an easy task. The steps are:

  1. Download the source package
The source package can be downloaded with setup.exe. The archive is found in directory /usr/src.
  1. Find the dependencies
Building a package requires that some extra development libraries be installed as well. These libraries are only needed for the build, and can be removed afterwards.
  1. Find which chain of commands to use


The main problem I encountered is the total lack of documentation in the source package. The same applies regarding any administration task in Cygwin (like how to install a package manually, etc). Obviously the usual way of ./configure; make && make install could work, but Cygwin often applies specific patches to packages. Also most packages have lots of configuration options. Clearly there must be some automated way to build the packages from the source.

The hint comes from the file with extension .cygport in the source package. See #cygport below.

Cygport

cygport is meant to simplify the building process of packages on cygwin. To build a package from source (assuming we have downloaded the source package with setup.exe), we simply issue the command:

cd /usr/src
cygport gvim-7.3.001-1.cygport almostall            # or 'all', will delete working directory

Cygport will:

  • Unpack the source archive
  • Apply upstream patches
  • Apply cygwin patches
  • Configure the package with a predefined set of option
  • Compile
  • Install (in local dir)
  • Create a source package and a binary package

All this is done in some local directory in /usr/src, where original sources are kept separate from the patched sources, build is not done in the source directory, etc. The structure of that local directory is:

/usr/src/gbim-7.3.003-1
build/                            # Directory where build is done. Check build/src/auto for configure log
config/                          
dist/                             # Packages ready for distribution
inst/                             # Content of Binary package
log/                              # Compile, install log
origsrc/                          # Original, unpatched sources 
patch/                            # patches
spkg/                             # Content of source package
src/                              # Source, patched
temp
CYGWIN-PATCHES

For more information, see cygport source package, man cygport, and [3].

Solving dependencies issues with cygcheck

Most build failure are due to unsatisfy dependencies, i.e. the package to build depends on some (development) libraries that are not installed or not found. Here some hints to solve that issue:

Look at compilation warnings / errors

This is probably the most obvious place to look at first. Compiler will give valuable hints about missing files (note that usually the compile log is saved in a file for later reference)

Look at Configure log

Look into the configure log file (config.log), and look for abnormal results (like X headers not found, etc). Then look in the configure file to see what is the corresponding test. This might give an interesting hint about which file is missing. For instance, in the case of vim, the configure script would compile a small program like the following to conclude about the presence of X headers:

#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
int
main ()
{
    ;
    return 0;
}

If the compilation fails, configure concludes that X headers are not available.

Find which package to install with cygcheck

Knowing the missing file, we can query the cygwin database for which packages provide that file. This is done with cygcheck:

cygcheck -p X11/Intrinsic.h
# Found 3 matches for X11/Intrinsic.h
# libXt-devel/libXt-devel-1.0.7-1 X.Org X Toolkit library (development)
# libXt-devel/libXt-devel-1.0.8-1 X.Org X Toolkit library (development)
# xorg-x11-devel/xorg-x11-devel-6.8.99.901-1      Obsolete package

Building Vim from source in Cygwin

Download the source package

  • Launch setup.exe, and check the source box (S) of gvim package.
    The package is downloaded in /usr/src. In our case we have:
  • 7.3.001 # These are 3 upstream patches to apply on vim-7.3.tar.bz2 7.3.002 7.3.003 gvim-7.3.003-1.cygport # The cygport script to build the package gvim-7.3.003-1.cygwin.patch # Cygwin patch to apply on vim-7.3.tar.bz2 (patched gvim-completion # Some additional file that will be copied upon install gvim.desktop-r2 vim-7.3.tar.bz2 # Original upstream package

Install the dependencies

Building a package requires that some extra development libraries be installed as well. These libraries are only needed for the build, and can be removed afterwards. To find which libraries to install, simply look at the file gvim-7.3.003-1.cygwin.patch. The important remarks is of course the one in bold:

Runtime requirements:
  alternatives
  cygwin-1.7.5-1                   
  libgdk_pixbuf2.0_0-2.20.1-1    ← + devel package!
  libglib2.0_0-2.24.1-1          ← + devel package!
  libgtk2.0_0-2.20.1-1           ← + devel package!
  libICE6-1.0.6-1                ← + devel package!
  libiconv2-1.13.1-1
  libintl8-0.17-11
  libncursesw10-5.7-18           ← + devel package!
  libpango1.0_0-1.28.1-2         ← + devel package!
  libSM6-1.1.1-2                 ← + devel package!
  libX11_6-1.3.5-1               ← + devel package!
  libXt6-1.0.8-1                 ← + devel package!
  vim-7.3.x

Build requirements:
+(besides corresponding devel packages)'
  binutils-2.20.51-2
  cygport-0.9.89-1
  gawk-3.1.7-1
  gcc4-core-4.5.0-1
  make-3.81-2
  pkg-config-0.23b-10
  sed-4.2.1-1

So to build the package, we must not only install the packages listed under "Build requirements", but also the devel version of runtime libraries. Note that not all runtime libraries have a development library.

Build the package

To build the package:

cd /usr/src
cygport gvim-7.3.001-1.cygport all

To change the configure settings, for instance to compile vim with ruby feature (+ruby), we need to add the option --enable-rubyinterp. The easiest is to edit the file gvim-7.3.003-1.cygport:

--- a/gvim-7.3.003-1.cygport    2011-07-27 14:35:31.765625000 +0200
+++ b/gvim-7.3.003-1.cygport    2011-07-27 16:51:34.390625000 +0200
@@ -25,7 +25,7 @@
 		CC="${CC} ${CFLAGS} -fno-strength-reduce -I/usr/include/ncursesw" \
 		LDFLAGS="${LDFLAGS} -L/usr/lib/ncursesw" \
 		CONF_ARGS="--prefix=/usr --with-vim-name=gvim --with-view-name=gview --with-global-runtime=/usr/share/vim" \
-		CONF_OPT_FEAT="--with-features=huge" \
+		CONF_OPT_FEAT="--with-features=huge --enable-rubyinterp" \
 		CONF_OPT_GUI="--enable-gui=gtk2 --disable-gtktest" \
 		CONF_OPT_COMPBY="\"--with-compiledby=Cygwin/X <cygwin-xfree@cygwin.com>\""
 }

Miscellaneous Tips

  • Interesting post about the 'su' command, explaining why there is no 'su' in Cygwin, and how 'ssh' overcomes this.
    • Other relevant post.
  • Use rxvt as terminal emulator instead of standard console window.
  • The + that appears next to file permission in ls output means that there are other permissions for the file (use getfacl on the file to see them)
-rwxr-x---+ 1 beq06659 Users 2555 Oct 10 23:32 inetd.conf*       #More permissions set (getfacl inetd.conf to see them)
-rwxr-x---  1 beq06659 Users  706 Nov  8 01:27 initscript*
  • Clipboard integration
    • To enable clipboard usage in Cygwin X Server, use option --clipboard with command xwin.
    • Device /dev/clipboard can be used for command-line manipulation of the clipboard (note that /dev/clipboard is not listed in /dev directory). The commands getclip and putclip can be used for the same purpose (package cygutils):
$ cat myfile >/dev/clipboard                         # To copy some text to the clipboard
$ cat myfile | putclip                               # ... same effect
$ cat /dev/clipboard                                 # To print the content of the clipboard
$ getclip                                            # ... same effect
  • Change the prefix used by cygwin to mount windows partitions (default is /cygdrive):
mount --change-cygdrive-prefix /win

Cygwin Useful commands

id

Use command id to print user identity

getclip

Read from the Windows clipboard and write it to standard output (same as cat /dev/clipboard)

getfacl

Display file and directory access control lists (ACLs).

putclip

Read standard input and write into windows clipboard (same as cat >/dev/clipboard)

regtool

  • regtool is the command-line equivalent of Regedit. It can be used to manipulate the content of the Windows registry. Some examples:
% regtool set -b '\prefix\KEY\VALUE' [data ...]   # REG_BINARY
% regtool set -e '\prefix\KEY\VALUE' [data ...]   # REG_EXPAND_SZ
% regtool set -i '\prefix\KEY\VALUE' [data ...]   # REG_DWORD
% regtool set -d '\prefix\KEY\VALUE' [data ...]   # REG_DWORD
% regtool set -m '\prefix\KEY\VALUE' [data ...]   # REG_MULTI_SZ
% regtool set -s '\prefix\KEY\VALUE' [data ...]   # REG_SZ
% regtool unset -q '\prefix\KEY\VALUE'            # -q quiet (no error output)
with prefix being HKCU, HKEY_CURRENT_USER, ... (see man regtool or regtool --help for more)
  • Check whether a KEY exists:
# in Bash...
% if ( regtool check -q '\HKEY_LOCAL_MACHINE\KEY' ); then echo Found; else echo Not Found; fi
# in CMD.EXE...
> regtool check -q '\HKEY_CURRENT_USER\KEY'
> if errorlevel 1 echo Not Found!
  • Check whether a VALUE exists:
# in Bash...
# ... we use list -l to limit output to KEYs!
% if ( regtool list -l -q '\prefix\KEY' | grep -q "^VALUE$" ); then echo Found; else echo Not Found; fi
% if ( regtool get -q '\prefix\KEY\VALUE' >/dev/null ); then echo Found; else echo Not Found; fi
# in CMD.EXE...
# ... we use list -l to limit output to KEYs!
> regtool list -l -q '\prefix\KEY' | find /i "VALUE" >\nul
> if errorlevel 1 echo Not Found!
> regtool get -q '\prefix\KEY\VALUE' >\nul
> if errorlevel 1 echo Not Found!
  • Beware of the use of back-slashes:
% regtool set -b '\HKEY_CURRENT_USER\Key\Value' [data ...]   # OK! works both for Cygwin and CMD.EXE
% regtool set -b '\\HKEY_CURRENT_USER\Key\Value' [data ...]  # NOK! Only works for CMD.EXE
% regtool set -b '\\HKEY_CURRENT_USER\\Key\Value' [data ...] # NOK! Only works for CMD.EXE

Cygwin Applications

rxvt

rxvt is a colour vt102 terminal emulator intended as an xterm replacement. It can be launched without starting any X Window System.

To install, simply add package rxvt.

To configure the default shell used by rxvt, define the following user variable as windows environment variable:

USER=/bin/bash

Appearance is controlled in file ~/.Xdefaults

rxvt*geometry:          160x50
rxvt*loginShell:        true
rxvt*font:              Lucida Console-12
rxvt*saveLines:         3000
rxvt*scrollBar_right:   true

Cygwin/X

Install:

  • Run setup.exe
  • Install the packages: xorg-server, xinit, xorg-docs, X-start-menu-icons, some font packages

Setup keyboard layout:

  • From the command-line, run the command:
startxwin.exe -- -xkblayout be
  • From Start Menu shortcut, edit the shortcut target (need to add quotes!):
C:\cygwin\bin\run.exe /usr/bin/bash.exe -l -c "/usr/bin/startxwin.exe -- -xkblayout be"