LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2009, 03:22 AM   #1
smartgupta
Member
 
Registered: Sep 2008
Posts: 81

Rep: Reputation: 15
Not able to lock the port


Hi,
I am working with Sun-Solaris 9 and in our application I need to connect the UPS with Serial port,after that I need to monitor it..but before stating communication,It should lock that port,To check it,first i am trying to make sure the lock directory exists and I am able to find it(/var/lock) but not able to lock the port and getting the error message from there---

LOCKDIR=/var/lock in PosixDevLock.c at 53
LOCKDIR=/var/lock in PosixDevLock.c at 57
Error message in PosixDevLock.c at 58
lckStatus value==>2

I am trying to lock the port using the function lock from other file and getting the value in lckStatus variable.
Code:
Quote:
int ChkPortIO (const char* portName) { printf(" \n Inside ChkPortIO at 82 ..........."); printf("\n 1..JNI_OK value==>%d",JNI_OK); struct termio pmode; /* modem device attributes */ int portID; /* modem port file descriptor */ int lckStatus; lckStatus = lock(portName); if (lckStatus) { printf("\n lckStatus value==>%d",lckStatus); return(lckStatus); } /* Open what is hopefully a real serial port. We use readonly access here // just in case it is something else like a file. */ portID = open(portName, O_RDONLY | O_NDELAY | O_NOCTTY); printf("\n Just before portID value==>%d",portID); if (portID < 0) { printf("\n Inside PortID error no ==>%d",errno); ulock(portName); return (errno); }

this lock function try to lock the port but during this i am not able to lock the port


Code:
Quote:
int lock(const char *line) { #ifdef POSIX_DEV_LOCK int mypid, rempid, i; int rc = -1; char tmpname[MAXPATHLEN], lckname[MAXPATHLEN], prodName[MAXPATHLEN]; char *p; FILE *f; /* First of all, make sure the lock directory exists. */ #ifdef AIX if (mkdir(LOCKDIR, S_IWUSR) != 0) #else printf("\n LOCKDIR=%s in PosixDevLock.c at 53",LOCKDIR); if (mkdir(LOCKDIR, S_IWUSR) != 0) #else printf("\n LOCKDIR=%s in PosixDevLock.c at 53",LOCKDIR); if (mkdir(LOCKDIR) != 0) #endif { printf("\n LOCKDIR=%s in PosixDevLock.c at 57",LOCKDIR); if (errno != EEXIST) { printf("\n Error message in PosixDevLock.c at 58 \n"); return(errno); } /* Get only the text after the last '/'. */ p = strrchr(line, '/'); if (p == NULL) { p = (char*)line; } else { p++; printf("\n value of p in 71 => %d",p); } /* Create a temporary lock file. */ mypid = getpid(); sprintf(tmpname, "%s%d", LCKTMP, mypid); printf("\n mypid at 78==>> %d",mypid); printf("\n tmpname at 79==> %s",tmpname); f = fopen(tmpname, "w"); if (f == NULL) { return(errno); } }


It should not go to the if statement(if (mkdir(LOCKDIR) != 0))but it is and returns the error no.-2 means(no such file or directory )...

Quote:
Inside ChkPortIO at 82 ...........
1..JNI_OK value==>0
LOCKDIR=/var/lock in PosixDevLock.c at 53
LOCKDIR=/var/lock in PosixDevLock.c at 57
Error message in PosixDevLock.c at 58
lckStatus value==>2

Please help me to resolve it...
Thanks in advance

Last edited by smartgupta; 02-10-2009 at 08:11 AM.
 
Old 02-10-2009, 07:31 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Can you please reformat the code that you have posted within CODE blocks. Highlight your code, then select the # symbol shown above the edit box.

I'm really confused with the #ifdef/#else in your code; there appears that there is not a closing #endif for one of the #ifdef statements.

If you need to print the line number in your printf() statements, use __LINE__. Try to avoid hard-coding the line number.

Why are you attempting to create the directory /var/lock? Does it not already exist on your system? If for some chance it doesn't, does your application have the appropriate run-time privilege to create the directory?

P.S. Use strerror() to yield the text-version of the error code returned by the system library calls. It might help in your analysis of your code.

Code:
#include <string.h>
#include <errno.h>
#include <stdio.h>

...

int status = mkdir(...);

if (status != 0)
{
  printf("Received error at line %d: %s\n", __LINE__, strerror(errno));
}

Last edited by dwhitney67; 02-10-2009 at 07:37 AM.
 
Old 02-10-2009, 08:18 AM   #3
smartgupta
Member
 
Registered: Sep 2008
Posts: 81

Original Poster
Rep: Reputation: 15
Thanks a lot...I will do it.

Yes,it already exist but for precaution,we are doing it..
Yes,application have the appropriate run-time privilege to create it..

but I am able to understand why It is going to if statement even directory is there and also don't have anything.

Quote:
/var
# ls -l lock
total 0
# cd lock
# pwd
/var/lock
# ls -l
total 0
# pwd
/var/lock
So it should not go to the if statement..So can you please help me to understand it and help me to resolve it...

Thanks in advance
 
Old 02-10-2009, 08:24 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
If the directory /var/lock already exists, and you attempt a mkdir(), you can expect it to fail. See the man-page for mkdir() (man 2 mkdir).

Code:
int rtn = mkdir("/var/lock", 01777);

if (rtn != 0)
{
  if (errno != EEXIST)
  {
    // grave error...
  }
}
P.S. You really should consider employing the use of stat() before attempting a mkdir(). If you are attempting to create a directory, and either a file or a directory with the same name already exists, mkdir() will not provide the details of this information. stat() on the other hand, can provide the details.

P.S.S. I experimented a little with mkdir(); be aware of your environment's umask setting. It will have an affect on the permissions set on the folder if it is created. Under Linux, the mode given is ANDed with the umask value and ANDed with 01777. This means apart from the permission bits, only the S_ISVTX mode bit is seen.

Last edited by dwhitney67; 02-10-2009 at 08:56 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable) phreakshew Linux - Newbie 24 04-12-2019 10:42 AM
all software installs hang scroll lock and caps lock lights flash jcorrnt Linux - Hardware 6 02-07-2009 12:44 PM
Hard lock plus Caps and Scroll Lock flashing (kernel panic?) TiredOfThis Linux - General 4 12-11-2007 07:35 PM
Allowing NFS in IPTABLES: Fix port for NFS Lock Manager Swakoo Linux - General 10 08-25-2006 05:24 AM
RedHat 9.0 freezes with blinking Caps lock and Scroll lock queen-bee Linux - Software 0 07-30-2004 10:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration